Demonstration of some of the string functions in ANSI Standard C
//---------------------------------------------------------------
// File: StringDemo.cpp
// Purpose: Demonstration of string functions available in through
// the string.h header file in ANSI standard C.
// Programming Language: C
// Author: Dr. Rick Coleman
//---------------------------------------------------------------
#include <stdio.h> // For printf() function
#include <string.h>
int main(int argc, char **argv)
{
char line[64]; // Define a char array to demonstrate C functions
printf("Demonstrating copying the string \"This is a test string.\"\n");
printf("\tinto the character array line[64]\n\n");
// Copy the string using strcpy
strcpy(line, "This is a test string.");
// Print the results
printf("line: %s\n", line);
// Also print the length of the string using strlen()
printf("Length = %d\n\n", strlen(line));
printf("\n\nPress [Enter] to continue.\n\n");
getchar();
printf("Demonstrating concatenating the string \" It uses string functions.\"\n");
printf("\tonto the character array line[64]\n\n");
// Copy the string using strcpy
strcat(line, " It uses string functions.");
// Print the results
printf("line: %s\n", line);
// Print the new length
printf("Length = %d\n\n", strlen(line));
printf("\n\nPress [Enter] to continue.\n\n");
getchar();
printf("Demonstrating strcmp()\n");
printf("Copying \"MNOP\" into line\n");
strcpy(line, "MNOP");
printf("Comparing to the string \"ABCD\" results = %d\n", strcmp(line, "ABCD"));
printf("Comparing to the string \"MNOP\" results = %d\n", strcmp(line, "MNOP"));
printf("Comparing to the string \"WXYZ\" results = %d\n", strcmp(line, "WXYZ"));
printf("\n\nPress [Enter] to continue.\n\n");
getchar();
printf("\n\nDemonstrating other string functions\n");
printf("Copying the string \"How do I program thee. Let me iterate the ways.\"");
printf(" into line.\n\n");
strcpy(line, "How do I program thee. Let me iterate the ways.");
printf("Results of call to strchr(line, 'I') = %s\n", strchr(line, 'I'));
printf("Results of call to strrchr(line, 'L') = %s\n", strrchr(line, 'L'));
printf("Results of call to strstr(line, \"the\") = %s\n", strstr(line, "the"));
printf("Results of call to strstr(line, \"love\") = %s\n", strstr(line, "love"));
return 0;
}