Strings in C
Subject: Data Structures and Algorithms · Language: C · Level: beginner · 6 min read
Work with character arrays, null terminator, strlen-style traversal, and string input.
A string is a group of characters written together. In C, a string is stored as a character array, and every valid string ends with a special character called the null character `\0`.
This is different from languages like Java or Python, where string is a separate built-in type. In C, you must remember that a string is handled through a `char` array.
For example, the word `campus` is stored in memory like this: `c a m p u s \0`. The `\0` tells C where the string ends.
Why Strings Are Important
Strings are used whenever a program works with text. Student names, passwords, messages, search keywords, file names, menu choices, and form data are all handled as strings.
Character vs String
A character stores one symbol. A string stores many characters.
- `char grade = 'A';` stores one character
- `char name[] = "Aman";` stores a string
Use single quotes for a character and double quotes for a string.
Declaring a String
A string is usually declared as a character array.
```c
char name[20];
```
This means `name` can store up to 19 visible characters, because one space is needed for `\0`.
Initializing a String
```c
char city[] = "Delhi";
```
Here C automatically adds `\0` at the end.
You can also initialize a string character by character.
```c
char city[] = {'D', 'e', 'l', 'h', 'i', '\0'};
```
Reading a Word
`scanf("%s", name);` reads one word. It stops reading when it finds a space.
```c
char name[50];
scanf("%49s", name);
```
The number `49` protects the array from receiving more characters than it can store. Notice that we do not write `&name` because the array name already represents the starting address.
Reading a Full Line
For a full sentence with spaces, `fgets` is safer than `scanf`.
```c
fgets(sentence, sizeof(sentence), stdin);
```
`fgets` can read spaces, so it is better for names like `Rahul Kumar` or full sentences.
Removing Newline after fgets
`fgets` may store the newline character when the user presses Enter. In real programs, you may need to remove it before comparing or printing the string.
This is one reason string handling in C needs careful attention.
Traversing a String
A string can be visited character by character until `\0` is found.
```c
while (word[i] != '\0') {
i++;
}
```
This pattern is used in many string problems such as finding length, counting vowels, checking palindrome, and comparing characters.
```c
#include <stdio.h>
int main(void) {
char word[50];
int length = 0;
int vowels = 0;
printf("Enter a word: ");
scanf("%49s", word);
while (word[length] != '\0') {
char ch = word[length];
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
vowels++;
}
length++;
}
printf("Length = %d\n", length);
printf("Vowels = %d\n", vowels);
return 0;
}
```
String Indexing
String indexing starts from 0, just like arrays.
- `word[0]` is the first character
- `word[1]` is the second character
- The last visible character comes before `\0`
String and Memory
If a string has 10 visible characters, C needs 11 character spaces because of the null character. This is why array size matters in string programs.
Important Points
- A C string is a `char` array
- Every valid string ends with `\0`
- Use `%s` for single-word input
- Use `fgets` when the input may contain spaces
- Do not forget array size while reading strings
- String comparison should be done using `strcmp`, not `==`
Useful String Functions
C provides common string functions in `string.h`.
- `strlen` finds length
- `strcpy` copies a string
- `strcmp` compares two strings
- `strcat` joins two strings
Comparing Strings
Do not compare two strings using `==`. That compares addresses, not the actual text.
Use `strcmp` for text comparison.
```c
if (strcmp(a, b) == 0) {
printf("Same");
}
```
Where Strings Are Used in DSA
Strings appear in palindrome checking, anagram problems, pattern matching, frequency counting, hashing, tries, and many interview questions.
Common String Problem Patterns
- Count characters, vowels, consonants, or digits
- Reverse a string
- Check palindrome
- Compare two strings
- Count frequency of each character
These patterns teach you how to move through a string using indexes and conditions.
Practice Question
Write a C program that takes one word as input and checks whether it is a palindrome.