I am getting this output:

This is the source string╠This is a destination string╠╠╠╠╠╠╠╠╠╠╠╠╠╠5Press any k
ey to continue . . .

My code is:

void Problem5(char strDestination [], char strSource [])

{
    int intIndexOfStrSource = 0;
    int intConcatenatedStringIndex = 0;
    int intStringDestinationIndex = 0;

    int intLengthOfDestination = strlen(strDestination);
    int intLengthOfSource = strlen(strSource);
    int intSizeOfConcatenatedString = intLengthOfDestination + intLengthOfSource;

    char strConcatenatedString[60];

    for(intIndexOfStrSource = 0; intIndexOfStrSource < intLengthOfSource ; intIndexOfStrSource += 1)        
    //Run loop from 0 to size of 1st array

    {
        strConcatenatedString[intIndexOfStrSource] = strSource[intIndexOfStrSource];
    }   

    for(intConcatenatedStringIndex = (intLengthOfSource + 1) , intStringDestinationIndex = 0; 
        intConcatenatedStringIndex < intLengthOfSource, intStringDestinationIndex < intLengthOfDestination ; 
        intConcatenatedStringIndex += 1 , intStringDestinationIndex += 1) 

        //Run loop from size end loop to size of 3nd array                                                         
        //Run loop from 2nd array to last of its size       
    {
        strConcatenatedString[intConcatenatedStringIndex] = strDestination[intStringDestinationIndex];
    }

    for(intStringDestinationIndex = 0; intStringDestinationIndex < strlen(strConcatenatedString); intStringDestinationIndex++)   

    {       
        printf("%c",strConcatenatedString[intStringDestinationIndex]) ; 
    }

    strConcatenatedString[intStringDestinationIndex] = '\0';

}

Why am I getting the garbage characters and how do I get rid of them?

Recommended Answers

All 7 Replies

In your second loop you have this:

intConcatenatedStringIndex = (intLengthOfSource + 1)

That's wrong, and it introduces an off-by-one error by setting intConcatenatedStringIndex one element too far. You'll have better results (and not an invervening garbage character) by initializing the index to intLengthOfSource without any changes.

On a side note, your excessively verbose naming convention is difficult to read. Compare your code with the following, which is pretty much identical except it uses shorter and more conventional names, and also has minor tweaks to maintain consistency (I also fixed a bug in the second loop's condition where only the latter condition was driving the loop):

void Problem5(char dst[], char src[])
{
    int dstlen = strlen(dst);
    int srclen = strlen(src);
    char result[60];
    int i, j;

    for (i = 0; i < srclen; i++)
    {
        result[i] = src[i];
    }

    for (i = srclen, j = 0; i < srclen && j < dstlen; i++, j++)
    {
        result[i] = dst[j];
    }

    for (i = 0; i < srclen + dstlen; i++)
    {
        printf("%c", result[i]); 
    }

    result[i] = '\0';
}

Finally, guessing that Problem5 really means writing your own version of strcat(), here's an implementation that's a bit more realistic and follows the interface and behavior of strcat() more closely:

char *concatenate(char dst[], const char *src)
{
    char *end = dst + strlen(dst);

    while (*end++ = *src++)
        ;

    return dst;
}

Not sure where precisely you are printing this string, but it appears to be outside of the Problem5 function. I see a printf statement inside of Problem5, but clearly you are having problems outside of your Print5 function (i.e. the funky character after "This is the source string". Line 37 is pointless. Your entire concatenated string array goes out of scope after Problem5, so you are adjusting it into a C-string at the end of the function, then not doing anything with it, don't bother. It's going to to go out of scope immediately. Also you apparently adding a NULL terminator to the string AFTER you use the strlen command. The strlen command NEEDS the NULL terminator to do its job, so stick line 37 before line 33. If the NULL terminator is in the right spot, you don't need a loop from 33 to 35. Just print the string with a "%s" specificier rather than a "%c".

The loop counter variables in line 21 looks off to me. That may be one of the stray character. Make sure that strConcatenatedString[intLengthOfSource] is initialized. Seems like that index might be getting skipped?

I tried your code

//Prototypes
void Problem8(char dst[], char src[]);

//Main Program

int main ()

{
    char src[] = "This is a source string";
    char dst[] = "This is a destination string";

    Problem8(dst, src)
}

//Definitions

void Problem8(char dst[], char src[])
{
    int dstlen = strlen(dst);
    int srclen = strlen(src);
    char result[60];
    int i, j;    

    for (i = 0; i < srclen; i++)
    {        
        result[i] = src[i];
    }

    for (i = srclen, j = 0; i < srclen && j < dstlen; i++, j++)

    {
        result[i] = dst[j];
    }    

    for (i = 0; i < srclen + dstlen; i++)    
    {        
        printf("%c", result[i]);     
    }    

    result[i] = '\0';
}

And I get this output:

This is the source string╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Press any key to continue
. . .

I'm using visual studio 2010.

So now I still get garbage and my strings aren't concatenating.
Thoughts?

for (i = srclen, j = 0; i < srclen && j < dstlen; i++, j++)

If i starts as srclen when will i < srclen be true? What happens to your loop in this case?

IMO, you need one more index value. Since you have srclen and dstlen you should have 1 more: resultlen

First Loop: resultlen starts at 0 and is incremented for each character added.
Second Loop: resultlen continues from where it left off...
Next: if you want a result string, where's the terminating \0 at the end of result?
Third loop: why use the complex printf() to output a single character when the simple function putchar() is created just for that purpose? Or if you actually make a result string, forget the loop and print the string itself.

My bad, I missed that bug too. Instead of comparing against srclen, it should compare i against the size of the result to avoid buffer overflow. But since this is clearly homework and that kind of robust error checking is both unnecessary and would result in further changes to the code, just use the condition to stop at the end of dst:

#include <stdio.h>
#include <string.h>

void Problem8(char dst[], char src[]);

int main(void)
{
    char src[] = "This is a source string";
    char dst[] = "This is a destination string";

    Problem8(dst, src);
    putchar('\n');

    return 0;
}

void Problem8(char dst[], char src[])
{
    int dstlen = strlen(dst);
    int srclen = strlen(src);
    char result[60];
    int i, j;

    for (i = 0; i < srclen; i++)
    {
        result[i] = src[i];
    }

    for (i = srclen, j = 0; j < dstlen; i++, j++)
    {
        result[i] = dst[j];
    }

    for (i = 0; i < srclen + dstlen; i++)    
    {
        printf("%c", result[i]);     
    }

    result[i] = '\0';
}

Hello Qonquest, unfortunately you no longer have the legal right to use the code you have written. By posting your code, you have granted DaniWeb an exclusive copyright license to your code according to DaniWeb's terms of service. You may no longer use it and have no rights to you code. Please delete your code from your computer. As the Terms of Service say:

Any and all information posted on DaniWeb may not be copied or used elsewhere, in any way, shape, or form, either on the Internet or in print, without prior written permission from Dani Horowitz.

Further transmission of your source code material, such as in a personal project or in handing in an assignment, may be prosecutable as criminal copyright infringement.

commented: yawn +0

Hello Qonquest, unfortunately you no longer have the legal right to use the code you have written. By posting your code, you have granted DaniWeb an exclusive copyright license to your code according to DaniWeb's terms of service. You may no longer use it and have no rights to you code. Please delete your code from your computer.

If you're trying to be funny, it's in very poor taste. If you disagree with the policy as it's written, please start a thread in the feedback forum to encourage changes rather than employing back stabbing techniques in places Dani isn't likely to see.

commented: well said +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.