Search code examples
c++ioc++14

how to removing rubbish data(garbage data) from output in c++?


Well, I have to Create class to Insert String in another one at the specific position Without using class string(any pre-made classes except iostream.

When I ask for output it gets rubbish data..

#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;
}

I used In cin.getline(), to get all string till '\0'. I traced the code all loops works fine except last one, Last one have garbage data.

I saw a video on youtube solved it like this but i couldn't trace it.. //This code is a working one, But i didn't use it since i couldn't trace it

#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;
 }

Solution

  • I cannot reproduce your garbage data but I see that you, in InsertStr() systematically forget, to add the zero at the end of strings. Your code goes in UB (Undefined Behaviour) so your garbage data a perfectly explained.

    So, copying s1 in s3

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

    Copying s2 to s1+pos

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

    And copying the last part of s3 at the end of s1

        for (int i = 0; i < len3; i++) {
            s1[x3] = s3[i];
            x3++;
        }
        s1[x3] = '\0'; // add this line!
    

    Or, maybe, you can simply write your loops, using do/while, as follows

    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++] );