/* Example of using string built in functions */
#include<stdio.h>
#include<string.h>
/* this library has the string functions in it */
int main()
{
char m1[50] = "Jack and Jill"; /*m1 has extra room to allow concatenation*/
char m2[]=" went up the hill";/*m2 has just enough room
for the number of
characters
in the string */
char m3[50];
char *m4; /*m4 is a
pointer to a character value */
printf("Original string 1 value: %s\n",m1);
printf("Original string 2 value: %s\n",m2);
printf("Original string 3 value: %s (note it has no
meaningful value here)\n\n",m3);
strcat(m1,m2); /*m2 is
concatenated to the end of m1 */
printf("strcat example(string 1 new value): %s\n\n",m1);
printf("strcpy example(string 1 is copied to string
3\n");
strcpy(m3,m1); /*m1 is
copied to m3*/
printf("String 1 value: %s\n",m1);
printf("String 3 value: %s\n\n",m3);
printf("strlen taken of string 3 %2d\n\n",strlen(m3));
printf("Difference between gets & scanf with string input
using a space\n");
printf("Enter a message that includes a space:");
gets(m3);
printf("\ngetf example:");
puts(m3); /*Note that with
a puts you cannot have any text output -
just
the string itself*/
printf("Enter another message that includes a space:");
scanf("%s",m3);
printf("\scanf example:");
puts(m3);
/* Comparing if two string have the same value */
printf("\nstrcmp example using string 1 and string 2 ");
if (strcmp(m1,m2)==0)
printf("\nString
1 is the same as String 2 \n");
else
printf("\nString
1 is NOT the same as String 2 \n");
printf("\n");
/* Assigment statement allowed because using a pointer */
m4="Direct assignment ok because it is declared a pointer
VARIABLE";
puts(m4);
/* The following statement would cause an error */
/* m3="Direct assignment is NOT allowed because m3 is a
pointer CONSTANT";*/
return 0;
}
Program Output
