Chapter 9: Strings in C
A string in C is simply a collection of characters (letters, numbers, symbols) that ends with a special character called null terminator (\0).
In C, there is no built-in string type like in Python or Java. Instead, a string is just a character array (char[]) that ends with \0.
1. String Declaration and Initialization
Declaration:
|
0 1 2 3 4 5 6 |
char str[20]; // Array of 20 characters (enough for 19 characters + \0) |
Initialization ways:
Way 1: Using double quotes (most common)
|
0 1 2 3 4 5 6 7 |
char name[20] = "Alex"; // Automatically adds \0 at the end char greeting[] = "Hello World"; // Compiler decides size (12 + \0 = 13) |
Way 2: Character by character (with null terminator)
|
0 1 2 3 4 5 6 |
char city[10] = {'H', 'y', 'd', 'e', 'r', 'a', 'b', 'a', 'd', '\0'}; |
Way 3: Without size (only when initializing)
|
0 1 2 3 4 5 6 |
char message[] = "Learning C is fun!"; // Size = 18 + \0 automatically |
Important Rule:
- Always leave one extra space for the \0 (null terminator)
- If you forget \0, many string functions will not work properly (they keep reading memory until they find \0)
2. String Input and Output
Output (Printing Strings): Use %s format specifier in printf()
|
0 1 2 3 4 5 6 |
printf("%s\n", name); |
Input (Reading Strings): Use scanf() or gets() or fgets() (recommended)
Using scanf() – reads until space
|
0 1 2 3 4 5 6 7 8 |
char name[50]; printf("Enter your name: "); scanf("%s", name); // NO & needed for array name! |
Problem: scanf(“%s”) stops at space → can’t read full name like “Alex Smith”
Better way: fgets() – reads whole line including spaces
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { char full_name[100]; printf("Enter your full name: "); fgets(full_name, 100, stdin); // Reads up to 99 chars + \0 printf("Hello, %s", full_name); return 0; } |
Note: fgets() includes the Enter key (\n) at the end – we can remove it if needed.
3. Important String Manipulation Functions
(You need to include <string.h> header)
| Function | Purpose | Example |
|---|---|---|
| strlen() | Returns length of string (without \0) | strlen(“Hello”) → 5 |
| strcpy() | Copies one string to another | strcpy(dest, src) |
| strcat() | Concatenates (joins) two strings | strcat(dest, src) |
| strcmp() | Compares two strings | Returns 0 if equal, negative/positive if different |
| strchr() | Finds first occurrence of character | strchr(“Hello”, ‘l’) → pointer to first ‘l’ |
| strstr() | Finds substring | strstr(“Hello World”, “World”) |
Example Program – All Important Functions
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <stdio.h> #include <string.h> int main() { char str1[50] = "Hello"; char str2[50] = " World!"; char copy[50]; char full[100]; // 1. strlen() printf("Length of '%s' = %zu\n", str1, strlen(str1)); // 5 // 2. strcpy() strcpy(copy, str1); printf("Copied string: %s\n", copy); // Hello // 3. strcat() strcpy(full, str1); // First copy Hello strcat(full, str2); // Then add " World!" printf("Concatenated: %s\n", full); // Hello World! // 4. strcmp() if (strcmp(str1, "Hello") == 0) { printf("str1 is equal to 'Hello'\n"); } if (strcmp(str1, "hello") != 0) { printf("str1 is NOT equal to 'hello' (case-sensitive)\n"); } // 5. Changing a character str1[0] = 'h'; printf("Modified str1: %s\n", str1); // hello return 0; } |
Output:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
Length of 'Hello' = 5 Copied string: Hello Concatenated: Hello World! str1 is equal to 'Hello' str1 is NOT equal to 'hello' (case-sensitive) Modified str1: hello |
4. More Real-Life String Examples
Example 1: Reverse a String
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> #include <string.h> int main() { char str[50]; int i, len; printf("Enter a string: "); fgets(str, 50, stdin); // Remove trailing newline if present str[strcspn(str, "\n")] = '\0'; len = strlen(str); printf("Reversed string: "); for (i = len - 1; i >= 0; i--) { printf("%c", str[i]); } printf("\n"); return 0; } |
Example 2: Count Vowels in a String
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <stdio.h> #include <string.h> #include <ctype.h> // for tolower() int main() { char str[100]; int i, count = 0; printf("Enter a sentence: "); fgets(str, 100, stdin); for (i = 0; str[i] != '\0'; i++) { char ch = tolower(str[i]); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { count++; } } printf("Number of vowels: %d\n", count); return 0; } |
Today’s Homework
- Write a program that takes a string from user and prints its length using strlen().
- Take two strings from user and join them using strcat() and print the result.
- Write a program to check if two strings are equal (using strcmp()).
- Create a program that takes a string and converts it to uppercase (use toupper() from <ctype.h>).
- Write a program to count how many times a particular character appears in a string.
