#include #include //Must be included for string manipulation int main(void){ char str1[80], str2[80]; // Note: '\0' is NULL printf("%d %d\n", '\0', NULL); puts("\nEnter the first string"); gets(str1); puts("Enter the second string"); gets(str2); // FINDING THE LENGTH OF A STRING // Note: The terminating null character is not included in the length printf("\nLength of string1 is %d\n", strlen(str1)); printf("Length of string2 is %d\n", strlen(str2)); // COMPARING A STRING TO ANOTHER STRING WITH CASE SENSITIVITY if(! strcmp(str1, str2)) printf("\nThe two strings are equal\n"); else printf("\nThe two strings are not equal\n"); // COMPARING A STRING TO ANOTHER STRING WITHOUT CASE SENSITIVITY //NOTE: stricmp IS NOT A STANDARD C FUNCTION, SOME C COMPILERS DO NOT SUPPORT IT // stricmp IS NOT IN THE ICS 103 SYLLABUS if(! stricmp(str1, str2)) printf("\nThe two strings are equal\n"); else printf("\nThe two strings are not equal\n"); // JOINING (CONCATENATING) A STRING TO ANOTHER STRING: strcat(str1, str2); printf("\nAfter concatenation:\n"); printf(" String1 is: %s\n", str1); printf(" String2 is: %s\n", str2); // COPYING A STRING TO ANOTHER STRING: strcpy(str1, "KFUPM IS MY UNIVERSITY\n"); printf("\nAfter string copy:\n"); printf(" String1 is: %s", str1); printf(" String2 is: %s", str2); // SEARCHING A CHARACTER IN A STRING: printf("\n\nEnter a string to be searched\n"); gets(str1); int ch; printf("\nEnter a character to search for: "); ch = getchar(); if(strchr(str1, ch)) printf("\n\n %c is in %s", ch, str1); else printf("\n\n %c is not in %s", ch, str1); fflush(stdin); // SEARCHING A STRING IN A STRING: printf("\n\nEnter a string to search for in %s : ", str1); gets(str2); if(strstr(str1, str2)) printf("\n%s is in %s\n", str2, str1); else printf("\n%s is not in %s\n", str2, str1); // TOKENIZING A STRING EXAMPLE1: char str[] = "now # is the time for all # ics 103 students # to start preparing # for the final"; char delims[] = "#"; char *token; token = strtok( str, delims ); while ( token != NULL ) { printf( "%s\n", token); token = strtok( NULL, delims ); } //NOTE strtok MODIFIES ITS FIRST ARGUMENT. WHAT REMAINS IN THE ARGUMENT IS THE FIRST TOKEN: puts(str); puts("========================================="); // TOKENIZING A STRING EXAMPLE2: char string[] = "now # is the time for all # ics 103 students # to start preparing # for the final"; char delimeters[] = "fsp"; char *t; t = strtok( string, delimeters ); while ( t != NULL ) { printf( "%s\n", t); t = strtok( NULL, delimeters ); } /* Comment the above while loop to check the effect of this portion: printf( "%s\n", t); char *t2; t2 = strtok( NULL, delimeters ); printf( "%s\n", t2); */ //NOTE strtok MODIFIES ITS FIRST ARGUMENT. WHAT REMAINS IN THE ARGUMENT IS THE FIRST TOKEN: puts(string); puts("========================================="); system("pause"); return 0; }