/** * Compare two string using strcmp * stringcmp.c */ #include #include #include int main (int argc, char *argv[]) { char str1[] = "this is"; char str2[] = "a test."; char *str3; char str4[10]; if (strcmp(str1, str2) == 0) { printf("Strings are equal. \n"); } else { printf("Strings are different. \n"); } // copy the contents of str1 into str4 // Then print all the characters, including the null terminator strcpy(str4, str1); int len = strlen(str4); for (int j = 0; j < len+1; j++) { printf("|%c|", str4[j]); } printf("\n"); // If we don't want to choose an arbirtrary size for a string (that we hope isn't too small) // Then we can malloc on the stack. We need to remember to // malloc space for the null-terminator, too. Hence, the +1. str3 =(char *)malloc( sizeof(char)*(strlen(str4)+1) ); strcpy( str3, str4 ); printf( "%s\n", str3 ); return 0; }