Firstly, i must mention that i'm just learning about strings in C as a beginner. What i want to do is get 2 strings as input from an user and concatenate them. So here's what i did:
char firststring[40], secondstring[40];
printf ("Enter first string: ");
fgets (firststring, 40, stdin);
printf ("Enter second string: ");
fgets (secondstring, 40, stdin);
strcat(firststring, secondstring);
printf("%s", firststring);
The problem is that fgets also reads the newline character when the user inputs the first string so the output looks like this:
Hello
World
I tried to use puts instead of fgets and it worked well, but too many people said NOT to use that function. Then i found out that i can use strcspn after the first fgets to remove the newline character, but that didn't gave me the one space i want between the words.
Desired output: Hello World
What i got: HelloWorld
Any suggestions how to do that?