I have made strcat() function myself but after adding the string it is printing an extra ascii symbol. Please tell why?
#include<stdio.h>
#include<conio.h>
#include<string开发者_StackOverflow.h>
void xstrcat(char string1[],char string2[]);
void main(void)
{ char x[100];
char string1[40],string2[40];
printf("Enter a string:");
gets(string1);
puts("Enter another string:");
gets(string2);
xstrcat(string1,string2);
printf("%s",string1);
getch();
}
void xstrcat(char string1[],char string2[])
{
int i,x,y;
x=strlen(string1);
y=strlen(string2);
for(i=0;i<y;i++)
{ string1[i+x]=string2[i];
}//for ends
}
Your xstrcat()
function isn't placing a null terminator character at the end of the resulting string.
One possible fix might be to put the following just before the xstrcat()
function returns:
string1[x+y] = '\0';
In C, strings are terminated with a NUL byte (character with value 0). strlen
will tell you how many characters there are from the beginning of the string until the NUL byte, not counting the NUL byte itself.
So when you execute this loop:
for(i=0;i<y;i++)
{ string1[i+x]=string2[i];
}
You never copy the terminating NUL byte from string2
into string1
, and so string1
no longer has a terminating NUL (you overwrote its NUL earlier in the loop with the first character of string2
). When a string is missing its terminating NUL, functions that read it (e.g. printf
) will continue reading past the string's intended endpoint until they do eventually find a NUL byte somewhere further along in memory. This can lead to printing extra characters and/or a crash.
Either change y
to y+1
or explicitly insert a '\0'
at position x+y
in string1
.
void xstrcat(char string1[],char string2[])
{
//int i,x,y;
size_t i, x, y;
// They could also be unsigned, but size_t is an unsigned big enough to hold the
// biggest in memory index possible
x=strlen(string1);
y=strlen(string2);
//for(i=0;i<y;i++)
for (i=0; i<=y; i++) // This picks up the null at the end
{ string1[i+x]=string2[i];
}//for ends
}
Alternately you could do this as:
void xstrcat(char * string1, const char * string2)
{
while(*string1) {
string1++;
}
strcpy(string1, string2);
}
This should be a little faster because it doesn't have to traverse either string but once. It also doesn't require as many extra variables.
your loop
for (i = 0; i < y; i++) {
string1[i+x]=string2[i];
}
you are reading string 2 up to its length-1 which will read only upto the last character before NULL, if you dont know in c or c++ strings are terminated with a NULL ( 0 )character.
So your string1 will not have a NULL character in the end.
So now when you try to print your string1 it will definitely print some characters in the end as your string was of size 40, these characters are garbage.
Mostly you will get a 40 character string printed except only when on of these garbage values is 0.
So you should read the second string upto 0, so make your loop
for (i = 0; i <= y; i++)
or you can simply add a NULL character in the end of string 1 after adding string2 into it.
string1[x+y] = 0;
精彩评论