/* strings.c Jeff Ondich, 26 April 2019 Part of a quick intro to C After you have gotten to know this program, try commenting out each of the #include statements one at a time to see which #include you need for each function. Also, try "man strlen" and "man toupper" and "man strlcpy", etc. in the terminal to see what the official documentation for the various standard library functions looks like. You can see the required #include statements there, among other things. */ #include #include #include int main() { char animal[10] = "moose"; printf("The animal is: %s\n", animal); printf("The length of the animal is %lu\n", strlen(animal)); // Copy. There are three many "copy one string to another" functions: // strcpy, strncpy, and strlcpy. You can search the internet for // "strncpy vs strlcpy" to get an idea of the history and technical // reasons we have this preposterous situation. In any case, we will // use strlcpy. strlcpy(animal, "goat", 10); printf("Now the animal is: %s\n", animal); // In C, strings are char arrays. The end of a string is marked // by a null character (which we write as '\0'), so you can iterate // over a string like this. for (int k = 0; animal[k] != '\0'; k++) { animal[k] = toupper(animal[k]); } printf("The new animal is now shouting: %s\n", animal); // You can do a lexicographic comparison (i.e. comparison like // in a dictionary, where all "a" words come before all "b" words, // and "abacus" comes before "ace", and "ace" comes before "acerbic". // strcmp returns < 0, > 0, or == 0 depending on the lexicographic // relationship between the first string and the second string. char a[] = "kudu"; char b[] = "coatimundi"; int comparison = strcmp(a, b); if (comparison < 0) { printf("%s comes before %s lexicographically\n", a, b); } else if (comparison > 0) { printf("%s comes after %s lexicographically\n", a, b); } else { printf("%s and %s are identical strings\n", a, b); } return 0; }