I'm new to C and C++ programming, can anyone give me a hint on what I'm doing wrong here. I'm trying to write to concat function that takes to pointers to chars and concatenates the second to the first. The code does do that, but the problem is that it adds a bunch of junk at the end.

For instance, when passing the arguments - "green" and "blue", the output will be "greenblue" plus a bunch of random characters. I also wrote the strlen function that strcat uses, which I will provide below it for reference. I'm using an online compiler The exact instructions and specification is this:


Code:
int main(int argc, char** argv)
{
const int MAX = 100;

char s1[MAX];
char s2[MAX];

cout << "Enter your first string up to 99 characters. ";
cin.getline(s1, sizeof(s1));
int size_s1 = strlen(s1);
cout << "Length of first string is " << size_s1 << "\n";

cout << "Enter your second string up to 99 characters. ";
cin.getline(s2, sizeof(s2));
int size_s2 = strlen(s2);
cout << "Length of second string is " << size_s2 << "\n";
cout << " Now the first string will be concatenated with the second
string ";
char* a = strcat(s1,s2);

for(int i = 0; i<MAX; i++)
cout <<a[i];

// system("pause");
return 0;
}

//strcat function to contatenate two strings
char* strcat(char *__s1, const char *__s2)
{
int indexOfs1 = strlen(__s1);
int s2L = strlen(__s2);
cout <<s2L << "\n";
int indexOfs2 = 0;
do{
__s1[indexOfs1] = __s2[indexOfs2];
indexOfs1++;
indexOfs2++;
}while(indexOfs2 < s2L);


return __s1;
}

//Returns length of char array
size_t strlen(const char *__s)
{
int count = 0;
int i;
for (i = 0; __s[i] != '\0'; i++)
count++;
return (count) / sizeof(__s[0]);

}
You have to keep in mind how strings in C/C++ are NUL-terminated.

When you append s2 to s1, you're overriding the NUL byte that terminates s1 with the first byte of s2. This is correct; but you also need to put a new NUL byte at the end of the concatenated result. This can be done by setting s2L = strlen(__s2) + 1 in strcat(), since s2 does have a NUL byte available to copy.

Code:
int s2L = strlen(__s2)+1;

This way, s1 will be properly terminated after concatenation.

But you also need to stop when you encounter that NUL byte while printing. This is because anything after the NUL byte is going to be undefined. You don't want to print the NUL byte to the console (even if it's invisible) and you also don't want to print whatever garbage there is at the end of your buffer.

Code:
for(int i = 0; i<MAX && s1[i] != '\0'; i++)
    cout <<a[i];

As quick side notes, make sure to indent your code as cleanly as possible. Also, the maximum size is not 99 characters for s1 and 99 characters for both, but rather 99 characters for the concatenation. If you overflow from the buffer you will get complete garbage as well. ^^
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement