Load the example program strings.c for an example of some ways to use strings.
main( )
{
char name1[12],name2[12],mixed[25];
char title[20];
strcpy(name1,"Rosalinda");
strcpy(name2,"Zeke");
strcpy(title,"This is the title.");
printf(" %s\n\n"title);
printf("Name 1 is %s\n",name1);
printf(Name 2 is %s\n",name2);
if(strcmp(name1,name2)>0) /* return 1 if name1 > name2 */
strcpy(mixed,name1);
else
strcpy(mixed,name2);
printf("The biggest name alphabetically is %s\n",mixed);
strcpy(mixed,name1);
strcat(mixed," ");
strcat(mixed,name2);
printf("Both names are %s\n",mixed);
}
First we define four strings. Next we come to a new function that you will find very useful, the "strcpy" function, or string copy. It copies from one string to another until it comes to the NULL character. It is easy to remember which one gets copies to which is you think of them like an assignment statement. Thus if you were to say, for example, "x = 23;", the data is copied from the right entity to the left one. In the "strcpy" function, the data is also copied from the right entity to the left, so that after execution of the first statement, name1 will contain the string "Rosalinda", but without the double quotes, they are the complier’s way of knowing that you are defining a string.
Likewise, "Zeke" is copied into "name2" by the second statement, then the "title" is copied. The title and both names are then printed out. Note that it is not necessary for the defined string to be exactly the same size as the string it will be called upon to store, only that it is at least as long as the string plus one more character for the NULL.
main( )
{
char name1[12],name2[12],mixed[25];
char title[20];
strcpy(name1,"Rosalinda");
strcpy(name2,"Zeke");
strcpy(title,"This is the title.");
printf(" %s\n\n"title);
printf("Name 1 is %s\n",name1);
printf(Name 2 is %s\n",name2);
if(strcmp(name1,name2)>0) /* return 1 if name1 > name2 */
strcpy(mixed,name1);
else
strcpy(mixed,name2);
printf("The biggest name alphabetically is %s\n",mixed);
strcpy(mixed,name1);
strcat(mixed," ");
strcat(mixed,name2);
printf("Both names are %s\n",mixed);
}
First we define four strings. Next we come to a new function that you will find very useful, the "strcpy" function, or string copy. It copies from one string to another until it comes to the NULL character. It is easy to remember which one gets copies to which is you think of them like an assignment statement. Thus if you were to say, for example, "x = 23;", the data is copied from the right entity to the left one. In the "strcpy" function, the data is also copied from the right entity to the left, so that after execution of the first statement, name1 will contain the string "Rosalinda", but without the double quotes, they are the complier’s way of knowing that you are defining a string.
Likewise, "Zeke" is copied into "name2" by the second statement, then the "title" is copied. The title and both names are then printed out. Note that it is not necessary for the defined string to be exactly the same size as the string it will be called upon to store, only that it is at least as long as the string plus one more character for the NULL.

