如何从c ++中的输出中删除垃圾数据(垃圾数据)?

时间:2018-04-14 08:50:43

标签: c++ io c++14

好吧,我必须创建类以在特定位置插入另一个字符串而不使用类字符串(除了iostream之外的任何预制类。

当我要求输出时,它会得到垃圾数据..

#include "stdafx.h"
#include <iostream>
using namespace std;enter code here
//StrLen Calculates String Length
int StrLen(char *s) {
    int len = 0;
    while (s[len]!='\0'){
        len++;

    }
    return len;
}
//The value of the string that is it's first argument is inserted into String that is it's second argument, pos is the beginning at position given by main.
char *InsertStr(char *s1, char *s2, int pos) {
    int c = 0;
    int len1 = StrLen(s1);
    int len2 = StrLen(s2);
    char s3[100];
    int i = pos;

    //This while move string from postion till the end of string to s3
    while (s1[i] != '\0') {
        s3[c] = s1[i];
        c++;
        i++;
    }
    c = 0;
    i = pos;
    //the string in s2[0 to the end of s2] moved to s1[from pos to len1]
    while (s2[c] != '\0') {
        s1[i] = s2[c];
        c++;
        i++;
    }
    int len3 = StrLen(s3);
        len1 = StrLen(s1);
        int x2 = len1 + len3;
        c = 0;
        len1 = StrLen(s1);
        int x3 = pos + len2;
        //this loop get the elements sent to s3 and get them back to     
        //s1[from pos + len2 till s3[i] reach the end]

        for (int i = 0; i < len3; i++) {
            s1[x3] = s3[i];
            x3++;
        }
    return s1;
}
int main()
{
    char s1[100];
    char s2[100];
    int pos=0;
    cout << "Enter The First Argument String : \n";
    cin.getline(s1, 100);
    cout << "Enter The Second Argument String : \n";
    cin.getline(s2, 100);
    cout << "Enter The Position : \n";
    cin >> pos;
    cout << InsertStr(s1, s2, pos);
    cout << "******************";

    return 0;
}

我使用In cin.getline()来获取所有字符串'\0'。 我跟踪代码所有循环工作正常,除了最后一个,最后一个有垃圾数据。

我在youtube上看到一个视频像这样解决了但是我无法追踪它。     //这个代码是一个有效的代码,但我没有使用它,因为我无法追踪它

#include "stdafx.h"
#include <iostream>
using namespace std;


int strlen(char *s)
{
    int c = 0;
    while (s[c] != '\0') c++;
    return c;
}

char* my_strncat(char *s1, char *s2, int pos)
{
    //k= pos
    int len1, len2;
    int i = 0, k = pos,l=0,j=0;
    int x,x1, x2, x3;

    len1 = strlen(s1);
    len2 = strlen(s2);
    char s3[100];
    while (i <= len1) {
        s3[i] = s1[i];
        i++;

    }
    //x2=len2 j =len1
    x1 = len1 + len2;
    x3 = len2 + pos;
    for (i = pos; i < x1; i++) {
        x = s3[i];
        if (l < len2) {
            s1[i] = s2[l];
            l++;

        }
        s1[x3] = x;
        x3++;
    }


    return s1;
}
int main()
{

    char s1[100] ;
    char s2[100] ;
    int  pos = 0;

    cout << "Enter The string of Source: \n";
    cin.getline(s1,100);

    cout << "Enter The string of Destination: \n";
    cin.getline(s2, 100);
    cout << "Enter The position of Destination: \n";
    cin >> pos;
    cout << my_strncat(s1, s2, pos) << endl;

    return 0;
 }

1 个答案:

答案 0 :(得分:0)

我无法重现您的垃圾数据,但我发现您在InsertStr()系统地忘记了在字符串末尾添加零。您的代码以UB(未定义的行为)进行,因此您的垃圾数据得到了完美的解释。

所以,在s1

中复制s3
while (s1[i] != '\0') {
    s3[c] = s1[i];
    c++;
    i++;
}
s3[c] = '\0'; // add this line!

s2复制到s1+pos

while (s2[c] != '\0') {
    s1[i] = s2[c];
    c++;
    i++;
}
s1[i] = '\0'; // add this line!

并在s3

的末尾复制s1的最后一部分
    for (int i = 0; i < len3; i++) {
        s1[x3] = s3[i];
        x3++;
    }
    s1[x3] = '\0'; // add this line!

或者,也许,您可以使用do / while简单地编写循环,如下所示

do
  s3[c++] = s1[i];
while ( s1[i++] );

// ...

do
   s1[i++] = s2[c];
while ( s2[c++] );

// ...

i = 0;
do
   s1[x3++] = s3[i];
while ( s3[i++] );