/** * @file 9_strings.c * @brief Part 1, Lesson 9: Strings * @author dunamismax * @date 06-15-2025 * * This lesson covers how to work with strings, which are one of the most * common types of data in programming. */ /* * ===================================================================================== * | - LESSON START - | * ===================================================================================== * * Up to this point, we've only seen string "literals" (text in double quotes) * used directly in `printf`. We haven't stored or manipulated them. * * THE BIG IDEA: C does not have a built-in `string` data type like many other * languages. Instead, a C STRING is simply an ARRAY of characters (`char`). * * But how does the computer know where the string ends in the array? * * THE NULL TERMINATOR * C strings follow a crucial convention: they are "null-terminated". This means * a special character, the NULL TERMINATOR `\0`, is placed after the last * actual character in the array to mark the end of the string. * * So, the string "Hello" is stored in a `char` array as: * ['H', 'e', 'l', 'l', 'o', '\0'] * It requires 6 characters of storage, not 5! This is a very important concept. * * All standard C functions that work with strings rely on this `\0` character. */ #include #include // We must include this new header for string manipulation functions. int main(void) { // --- Part 1: Declaring and Printing Strings --- /* * The easiest way to declare and initialize a string is to use a string * literal. The compiler automatically adds the null terminator `\0` for you. */ char greeting[] = "Hello, C programmer!"; printf("--- Part 1: Declaring and Printing Strings ---\n"); /* * The `%s` format specifier in `printf` is used for strings. When `printf` * receives a `char` array with `%s`, it prints characters one by one * until it finds the null terminator `\0`. */ printf("Our greeting string: %s\n", greeting); // Let's prove the null terminator exists. We'll manually initialize one. // This is the "hard way" but illustrates the concept perfectly. char name[] = {'A', 'l', 'i', 'c', 'e', '\0'}; printf("A manually created name: %s\n\n", name); // --- Part 2: The `` Library --- /* * Because strings are so common, C provides a standard library of functions * to work with them. We included `` at the top to get access. */ printf("--- Part 2: String Functions ---\n"); // `strlen()` - String Length // This function counts the number of characters in a string *before* the // null terminator. char message[] = "This is a test."; // The length is 15, even though the array size is 16 (to hold the `\0`). printf("The message is: \"%s\"\n", message); printf("The length of the message (strlen) is: %zu\n", strlen(message)); // Note: strlen returns a special unsigned integer type `size_t`. We use // the `%zu` format specifier to print it correctly and avoid warnings. printf("\n"); // --- Part 3: Copying and Concatenating Strings --- printf("--- Part 3: Copying and Concatenating ---\n"); /* * `strcpy()` - String Copy * You CANNOT assign one string to another using `=`. * `char another_string[20]; another_string = message;` <-- THIS IS WRONG! * * You must copy the characters from the source to the destination. * WARNING: The destination array must be large enough to hold the source * string, including its null terminator! * * `strcpy` does NOT check this for you. It keeps copying until it reaches * the source's `\0`, even past the end of the destination. Writing past * the end of an array is called a BUFFER OVERFLOW. It is undefined * behavior and one of the most common security bugs in C programs. * * Here it is safe because we chose the sizes: `source` holds 24 characters * (23 of text plus the `\0`) and `destination` holds 50. When the source comes * from the user or a file, you cannot know its length in advance: check it * with `strlen()` before copying (Lesson 17 does this), or read it with a * function that is told the destination's size, such as `fgets` (Lesson 14). */ char destination[50]; // A buffer to copy into. char source[] = "Some text to be copied."; strcpy(destination, source); // Copies `source` into `destination`. printf("Result of strcpy: %s\n", destination); /* * `strcat()` - String Concatenate (join) * This function appends one string to the end of another. * WARNING: Again, the destination string must have enough space for the * combined result! `strcat` does not check either. "Welcome, " (9 characters) * plus "dunamismax" (10) plus `\0` needs 20 characters, and we have 50. */ char full_greeting[50] = "Welcome, "; // Initialize with enough space. char user[] = "dunamismax"; strcat(full_greeting, user); // Appends `user` to `full_greeting`. printf("Result of strcat: %s\n\n", full_greeting); // --- Part 4: Comparing Strings --- printf("--- Part 4: Comparing Strings ---\n"); /* * `strcmp()` - String Compare * You CANNOT compare strings using `==`. * `if (str1 == str2)` <-- THIS IS WRONG! It compares memory locations, not content. * * `strcmp()` compares two strings lexicographically (like in a dictionary). * It returns: * - 0 if the strings are identical. * - A negative value if the first string comes before the second. * - A positive value if the first string comes after the second. */ char pass1[] = "password123"; char pass2[] = "Password123"; // Case matters! char pass3[] = "password123"; if (strcmp(pass1, pass2) == 0) { printf("\"%s\" and \"%s\" are the same.\n", pass1, pass2); } else { printf("\"%s\" and \"%s\" are different.\n", pass1, pass2); } if (strcmp(pass1, pass3) == 0) { printf("\"%s\" and \"%s\" are the same.\n", pass1, pass3); } return 0; } /* * ===================================================================================== * | - LESSON END - | * ===================================================================================== * * Congratulations on completing Part 1! Strings are a huge concept. * * Key Takeaways: * - A C string is a `char` array ending with a null terminator `\0`. * - The `%s` format specifier is used to print strings. * - The `` header provides essential functions for strings. * - `strlen()` gets the length (not including `\0`). * - `strcpy()` copies one string to another. * - `strcat()` joins one string to the end of another. * - Neither `strcpy()` nor `strcat()` checks the destination's size. Make sure * it is big enough, or you get a BUFFER OVERFLOW. * - `strcmp()` compares the content of two strings. * * HOW TO COMPILE AND RUN THIS CODE: * * 1. Open a terminal or command prompt. * 2. Navigate to the directory where you saved this file. * 3. Use the GCC compiler to create an executable file: * `gcc -Wall -Wextra -std=c17 -o 9_strings 9_strings.c` * 4. Run the executable: * - On Linux/macOS: `./9_strings` * - On Windows (WSL): `./9_strings` */