Anyone know how to copy to strings? Cause I used the function strcpy but when I print the result it show strange characters. I want to concatenate 'name' + '@' + 'e-mail'. With scanf I have to put the character null '\0'?
#include <stdio.h>
#include <string.h>
int main (){
char message[150];
char name[150];
char mail[150];
char result[150];
printf("Introduce name: \n");
scanf("%s",message);
printf("Introduce email \n");
scanf("%s",server);
strcpy(result,message);
result[strlen(result)]='@';
strcpy(&result[strlen(result)],server);
printf("RESULT: %s\n",result);
return 0;
}
result[strlen(result)]='@';overwrites the NUL terminator introduced intoresultbystrcpy(result,message);. So the result of a subsequentstrlenis undefined.A better solution is to use
strncat, or you could get away with writingchar result[150] = {'\0'};which will initialise the entire array.
But you still run the risk of overflowing your
resultarray. You could use the saferstrncpyto obviate that. Better still, usesnprintfand have the C standard library perform the concatenation for you.