Strings in C Programming (2026 Guide) – Syntax, Functions, Examples & Practice Programs for Beginners
Before learning Strings in C Programming, it is highly recommended to understand these important topics:
👉 Variables In C
👉 Functions In C
👉 Arrays In C
👉 Pointers In C
Why?
Because strings in C are closely connected with arrays and pointers.
Arrays store multiple values.
Characters are single letters like ‘A’, ‘B’, or ‘C’.
Strings are collections of characters stored together.
What is a String in C?
A string in C is a collection of characters stored inside a character array.
Example:
char name[] = "Hello";
Here:
- H → character
- e → character
- l → character
- l → character
- o → character
- \0 → null character (end of string)
Why Do We Need Strings in C?
In real-world programming, we often work with text data.
- Student names
- Passwords
- Messages
- Emails
- Addresses
- Search keywords
Strings help us:
- ✔ Store text easily
- ✔ Process characters efficiently
- ✔ Build user-friendly applications
- ✔ Handle input and output operations
Syntax of Strings in C
char string_name[size];
Example:
char name[20];
- char → character data type
- name → string variable name
- 20 → maximum size of string
How to Initialize Strings
Method 1
char name[] = "Hello";
Method 2
char name[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
Method 3
char name[20] = "Programming";
Reading Strings in C
Strings can be entered using:
- ✔ scanf()
- ✔ gets()
- ✔ fgets()
Using scanf()
char name[20];
scanf("%s", name);
Using gets()
gets(name);
Using fgets() (Recommended)
fgets(name, sizeof(name), stdin);
Displaying Strings
Using printf()
printf("%s", name);
Using puts()
puts(name);
String Functions in C
C provides many built-in string functions in string.h.
#include <string.h>
strlen() – Find Length of String
char name[] = "Hello";
printf("%d", strlen(name));
strcpy() – Copy String
char str1[] = "Hello";
char str2[20];
strcpy(str2, str1);
printf("%s", str2);
strcat() – Concatenate Strings
char str1[20] = "Hello ";
char str2[] = "World";
strcat(str1, str2);
printf("%s", str1);
strcmp() – Compare Strings
char str1[] = "Apple";
char str2[] = "Apple";
printf("%d", strcmp(str1, str2));
strrev() – Reverse String
char str[] = "Hello";
strrev(str);
printf("%s", str);
Accessing String Characters
char name[] = "Hello";
printf("%c", name[1]);
Strings and Loops in C
char name[] = "Hello";
int i;
for(i = 0; name[i] != '\0'; i++)
{
printf("%c\n", name[i]);
}
Strings and Pointers in C
char *ptr = "Hello";
printf("%s", ptr);
Two-Dimensional String Array
char names[3][20] = {
"John",
"Alice",
"David"
};
Advantages of Strings in C
- ✔ Easy text handling
- ✔ Efficient memory usage
- ✔ Useful for input/output
- ✔ Supports many built-in functions
- ✔ Widely used in real applications
Limitations of Strings in C
- Fixed size arrays
- Manual memory management
- Risk of buffer overflow
- String handling can become complex
Practice Programs on Strings
🟢 Easy Level:
- Program to find length of string
- Program to display string characters
🟡 Medium Level:
- Program to copy string
- Program to compare two strings
🔴 Advanced Level:
- Program to reverse string
- Program to check palindrome string
- Program to count vowels and consonants
Real-World Uses of Strings
- Login systems
- Search engines
- Chat applications
- Databases
- Web development
- File handling
- Text editors
Difference Between Character and String
| Character | String |
|---|---|
| Single letter | Collection of characters |
| Uses single quotes | Uses double quotes |
| Example: 'A' | Example: "Apple" |
Conclusion
✔ Process characters
✔ Build interactive programs
✔ Handle real-world applications
FAQ – Strings in C
Q1: What is a string in C?
A string is a collection of characters ending with \0.
Q2: Which data type is used for strings?
char array.
Q3: Which header file is used for string functions?
string.h
Q4: What does strlen() do?
It finds the length of a string.
Q5: Why is \0 important in strings?
It marks the end of the string.
Keep practicing and keep coding 💛

Comments
Post a Comment