Strings in C
So far, you've learned how to store numbers using variables and arrays. But what if you need to store text such as a person's name, email address, city, or password?
Instead of storing each character separately, C provides strings, which allow us to store and manipulate text efficiently.
A string is one of the most frequently used data types in programming. Applications like chat systems, login forms, search engines, compilers, and databases all rely heavily on strings.
🎯 Learning Objectives
After completing this lesson, you will be able to:
- Understand what strings are.
- Learn how strings are stored in memory.
- Declare and initialize strings.
- Read and print strings.
- Use common string functions.
- Compare, copy, and concatenate strings.
- Avoid common mistakes while working with strings.
📖 What is a String?
A string is a sequence of characters terminated by a special character called the null character ('\0').
Unlike integers and floating-point numbers, strings are stored as arrays of characters.
Example
cchar name[] = "KauraX";
Memory representation:
text+---+---+---+---+---+---+----+ | K | a | u | r | a | X | \0 | +---+---+---+---+---+---+----+
The null character marks the end of the string.
Why is the Null Character Important?
The C compiler doesn't know where a string ends unless it encounters '\0'.
Example
cchar name[] = "Hi";
Memory:
textIndex : 0 1 2 Value : H i \0
Without the null character, functions like printf() would continue reading memory beyond the string, leading to undefined behavior.
Declaring Strings
Method 1
cchar name[20];
Allocates space for 20 characters.
Method 2
cchar name[] = "KauraX";
The compiler automatically determines the size.
Method 3
cchar name[] = {'K','a','u','r','a','X','\0'};
Equivalent to the previous declaration.
Reading Strings
Using scanf()
c#include <stdio.h> int main() { char name[30]; printf("Enter your name: "); scanf("%s", name); printf("Hello %s", name); return 0; }
Output
textEnter your name: KauraX Hello KauraX
Reading Strings with Spaces
Use fgets() instead.
c#include <stdio.h> int main() { char name[50]; printf("Enter Full Name: "); fgets(name, sizeof(name), stdin); printf("%s", name); return 0; }
Printing Strings
cprintf("%s", name);
The %s format specifier is used for strings.
Accessing Individual Characters
Since strings are arrays, each character has an index.
Example
cchar course[] = "KauraX"; printf("%c", course[0]);
Output
textK
Modifying Characters
cchar name[] = "KauraX"; name[0] = 'k'; printf("%s", name);
Output
textkauraX
Standard String Library
To use string functions, include:
c#include <string.h>
strlen()
Returns the length of a string (excluding '\0').
Example
c#include <stdio.h> #include <string.h> int main() { char name[] = "KauraX"; printf("%lu", strlen(name)); return 0; }
Output
text6
strcpy()
Copies one string into another.
c#include <stdio.h> #include <string.h> int main() { char source[] = "KauraX"; char destination[20]; strcpy(destination, source); printf("%s", destination); return 0; }
Output
textKauraX
strcat()
Appends one string to another.
c#include <stdio.h> #include <string.h> int main() { char first[30] = "Hello "; strcat(first, "KauraX"); printf("%s", first); return 0; }
Output
textHello KauraX
strcmp()
Compares two strings.
c#include <stdio.h> #include <string.h> int main() { char a[] = "Apple"; char b[] = "Apple"; printf("%d", strcmp(a, b)); return 0; }
Output
text0
Meaning:
0→ Equal< 0→ First string is smaller> 0→ First string is greater
Common String Operations
Reverse a String
Example logic:
textInput: KauraX Output: XaruaK
Count Vowels
Input
textProgramming
Output
textVowels = 3
Count Characters
cstrlen(name)
Check Palindrome
Example
textmadam
Output
textPalindrome
String vs Character Array
| Character | String |
|---|---|
'A' | "Apple" |
Stored in char | Stored in char[] |
Uses %c | Uses %s |
Real-World Example
c#include <stdio.h> int main() { char student[] = "Pritee"; printf("Welcome to KauraX\n"); printf("Student : %s", student); return 0; }
Output
textWelcome to KauraX Student : Pritee
Common Beginner Mistakes
Forgetting Space for Null Character
Wrong
cchar name[5] = "Hello";
Correct
cchar name[6] = "Hello";
Using == for String Comparison
Wrong
cif(name == anotherName)
Correct
cif(strcmp(name, anotherName) == 0)
Using gets()
Avoid
cgets(name);
It is unsafe and has been removed from the C standard.
Use
cfgets()
instead.
Best Practices
- Always allocate enough memory for the null character.
- Use
fgets()instead ofgets(). - Use
strcmp()for comparison. - Include
<string.h>when using string functions. - Validate user input whenever possible.
🧠 Interview Questions
- What is a string in C?
- Why is the null character important?
- Differentiate between a character and a string.
- Explain
strlen(). - Explain
strcpy(). - Explain
strcmp(). - Explain
strcat(). - Why is
gets()unsafe?
✍ Practice Questions
- Read and print a string.
- Find the length of a string.
- Reverse a string.
- Count vowels and consonants.
- Check whether a string is a palindrome.
- Compare two strings.
- Copy one string into another.
- Concatenate two strings.
- Count words in a sentence.
- Convert lowercase letters to uppercase.
📚 Lesson Summary
In this lesson, you learned:
- What strings are.
- How strings are stored in memory.
- Declaring and initializing strings.
- Reading and printing strings.
- Standard string functions (
strlen(),strcpy(),strcat(),strcmp()). - Common mistakes and best practices.
Strings are one of the most important concepts in C because almost every application processes text in some form. Mastering strings is essential before learning pointers and advanced data structures.
🚀 What's Next?
In the next lesson, Pointers in C, you'll learn:
- Memory addresses
- Pointer declaration
- Pointer arithmetic
- Pointers and arrays
- Pointers and functions
- Dynamic memory basics
- Practical examples
- Interview questions
Pointers are one of the most powerful and challenging concepts in C, enabling efficient memory management and advanced programming techniques.
Happy Coding with KauraX 💙