好吧,我必须创建一个类,以便在不使用类字符串的情况下,在特定位置的另一个位置插入字符串(除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;
}

我在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;
 }

最佳答案

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

因此,复制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++] );

关于c++ - 如何从C++输出中删除垃圾数据(垃圾数据)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49829596/

10-09 18:28