C: Strings - Strcat() (Page 4 of 6 )
If you wish to concatenate or join two strings together, you can use the strcat() function, like so:
#include <stdio.h>
#include <string.h>
int main ()
{
char first[80]="Yippie";
char second[90]="Hooray";
char spacer[5]="n";
printf(first);
printf(spacer);
strcat_s(first,second);
printf(first);
printf(spacer);
printf(second);
return(0);
}
This program creates three arrays. The first one, aptly named first, stores the value “Yippie.” The second array, named (you guessed it) second, holds the value “Hooray.” The third, titled “Spacer,” is used to insert a blank line between the text we will be printing.
We start off printing the value of first, and then we print out the spacer. Next we use strcat_s() to join the values of first and second together. Note that this value gets stored in first, and the value of second stays the same. We then print out first again, to show its new value, then a spacer, and finally the array second, to show that it has not changed. The result is:
Yippie
YippieHooray
Hooray
Next: Strcmp() >>
More BrainDump Articles
More By James Payne