💡 Strings in C Language – A Comprehensive Guide
Strings are one of the most important data types in the C programming language. They are used to store and manipulate text, such as words, sentences, or any sequence of characters. In C, strings are represented as arrays of characters, ending with a special null character '\0'.
🔹 What Is a String in C?
A string in C is basically a character array terminated by a null character ('\0').
It is not a built-in data type, but rather a convention used for handling sequences of characters.
// Example: Declaring and Initializing a String
#include <stdio.h>
int main() {
char name[10] = "C Language";
printf("String: %s", name);
return 0;
}
Here, the compiler automatically adds '\0' at the end of the string to mark its termination.
🔹 Declaring Strings in C
You can declare strings in multiple ways. The most common are:
- Character array with initialization
- Character pointer
// 1. Using character array
char city[20] = "New York";
// 2. Using character pointer
char *country = "India";
🔹 Accessing Characters in a String
Each character in a string can be accessed using its index position, starting from 0.
#include <stdio.h>
int main() {
char word[] = "Hello";
printf("%c", word[1]); // prints 'e'
return 0;
}
🔹 Common String Functions in C
C provides several useful functions in the <string.h> library for manipulating strings:
| Function | Description | Example |
|---|---|---|
strlen() |
Returns the length of a string | strlen("Hello") → 5 |
strcpy() |
Copies one string into another | strcpy(dest, src) |
strncpy() |
Copies first n characters of one string into another | strncpy(dest, src, 3) → copies first 3 chars |
strcat() |
Concatenates (joins) two strings | strcat(str1, str2) |
strncat() |
Concatenates up to n characters from one string to another | strncat(str1, str2, 3) |
strcmp() |
Compares two strings (returns 0 if equal) | strcmp("abc","abc") → 0 |
strchr() |
Finds the first occurrence of a character | strchr("Hello", 'l') → "llo" |
strrchr() |
Finds the last occurrence of a character | strrchr("Hello", 'l') → "lo" |
strstr() |
Finds the first occurrence of a substring | strstr("Programming","gram") → "gramming" |
strtok() |
Splits a string into tokens using a delimiter | strtok("A,B,C", ",") → "A" |
strrev() |
Reverses a string (available in some compilers) | strrev("C") → "C" |
strupr() / strlwr() |
Converts string to upper or lower case (compiler-dependent) | strupr("hello") → "HELLO" |
🔹 Example Program Using String Functions
#include <stdio.h>
#include <string.h>
int main() {
char s1[20] = "Hello";
char s2[20] = "World";
char s3[40];
strcpy(s3, s1); // copy s1 into s3
strcat(s3, s2); // concatenate s2 with s3
printf("Combined: %s\n", s3);
printf("Length: %lu\n", strlen(s3));
if (strcmp(s1, s2) == 0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}
🔹 Difference Between Character Array and Pointer String
- Character arrays can be modified, but string literals (pointers) cannot.
- Strings defined as pointers are stored in read-only memory.
char str1[] = "Hello"; // modifiable
char *str2 = "Hello"; // not modifiable
🔹 Tips for Working with Strings
- Always leave space for the null character
'\0'when declaring strings. - Use functions from
<string.h>instead of writing custom loops whenever possible. - Be careful with buffer overflows when using
strcpy()orstrcat().
🧩 String Handling Exercises in C
Practice these questions to strengthen your understanding of string manipulation in C —
covering basic operations, library functions, and logic-based programs.
- Basic: Write a C program to
find the length of a string using
strlen()function. - Basic: Write a program to
copy one string into another without using
strcpy()(use loops). - Basic: Write a program to
concatenate two strings without using
strcat(). - Intermediate: Write a program to
compare two strings using both
strcmp()and your own logic
(character by character comparison). - Intermediate: Write a program to
count the number of vowels and consonants in a string. - Intermediate: Write a C program to
reverse a string using
strrev()and also manually using loops. - Intermediate: Write a program to
check whether a string is a palindrome or not
(e.g.,"madam"). - Advanced: Write a program to
count the number of words in a string entered by the user. - Advanced: Write a program to
find and replace a word in a sentence
(e.g., replace “world” with “C” in"Hello world"). - Advanced: Write a program to
split a string into tokens using
strtok()
(for example, break a comma-separated list into words).
Tip: Try solving each question first manually,
then re-implement using built-in functions from
<string.h> to understand both logic and
library-based approaches.
✅ Conclusion
Strings in C may seem tricky at first because they are represented as character arrays, but once you understand how they are stored and manipulated, they become very powerful tools for text processing. Mastering string handling functions will make your C programs more dynamic and efficient.
