Strings in C++ – Complete Tutorial
Last Updated on: 10th Dec 2025 17:21:47 PM
In programming, handling text such as names, messages, passwords, sentences, and paragraphs is very common. Strings are used in C++ to store and manipulate textual data efficiently.
C++ provides two different ways to work with strings:
-
Character Arrays (C-style strings)
-
string class (C++ Standard Library)
Understanding both is very important for academic exams, interviews, and real-world applications.
What is a String in C++ ?
A string in C++ is a sequence of characters used to represent textual data such as words, sentences, or paragraphs.
Key Characteristics
-
Stores multiple characters
-
Ends with a null character (
'\0') in C-style strings -
Can be manipulated using built-in functions
-
Used for text processing
Why Strings Are Needed
Without strings:
-
Handling text becomes complex
-
Each character must be stored separately
-
No built-in functions for manipulation
Strings are used for:
-
User names
-
Passwords
-
Messages
-
File names
-
URLs
-
Search and text processing
Types of Strings in C++
C++ supports two types of strings:
| Type | Description |
|---|---|
| Character Array | Traditional C-style strings |
| string Class | Modern, flexible, and safe |
PART-A: Character Array ( C-Style Strings )
1. Character Array (C-Style String)
A character array string is an array of characters ending with a null character '\0', which indicates the end of the string.
Declaration of Character String
Syntax
char str[size];
Example
char name[20];
Initialization of Character String
char name[] = "Cplusplus";
Memory representation:
C p l u s p l u s \0
Important Rule
C-style strings must end with '\0' to mark the end.
Input and Output of Character String
Using cin
char name[20];
cin >> name;
cout << name;
⚠ Stops input at space
Using cin.getline()
char name[50];
cin.getline(name, 50);
✔ Reads full line including spaces
Important Difference: cin vs getline
| Feature | cin | getline |
|---|---|---|
| Reads spaces | ❌ No | ✅ Yes |
| Reads full sentence | ❌ | ✅ |
| Common use | Words | Sentences |
String Handling Functions ( <cstring> )
Include header:
#include <cstring>
Common String Functions
| Function | Description |
|---|---|
strlen() |
Length of string |
strcpy() |
Copy string |
strcat() |
Concatenate strings |
strcmp() |
Compare strings |
strlwr() |
Convert to lowercase |
strupr() |
Convert to uppercase |
Example: strlen()
char s[] = "Hello";
cout << strlen(s); // Output: 5
Example: strcpy()
char a[10], b[] = "Hi";
strcpy(a, b);
cout << a;
Example: strcmp()
if (strcmp("abc", "abc") == 0)
cout << "Equal";
Example Program – C-Style String Functions (One Program)
#include <iostream>
#include <cstring> // Required for string functions
using namespace std;
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
char str3[50];
// 1. strlen() – find length of string
cout << "Length of str1 using strlen(): " << strlen(str1) << endl;
// 2. strcpy() – copy string
strcpy(str3, str1);
cout << "After strcpy(), str3: " << str3 << endl;
// 3. strcat() – concatenate strings
strcat(str1, " ");
strcat(str1, str2);
cout << "After strcat(), str1: " << str1 << endl;
// 4. strcmp() – compare strings
if (strcmp(str2, "World") == 0)
cout << "str2 and \"World\" are equal" << endl;
else
cout << "Strings are not equal" << endl;
// 5. strlwr() – convert to lowercase
cout << "Lowercase str1: " << strlwr(str1) << endl;
// 6. strupr() – convert to uppercase
cout << "Uppercase str2: " << strupr(str2) << endl;
return 0;
}
Output (Expected)
Length of str1 using strlen(): 5
After strcpy(), str3: Hello
After strcat(), str1: Hello World
str2 and "World" are equal
Lowercase str1: hello world
Uppercase str2: WORLD
Limitations of Character Strings
1. Fixed size
2. Buffer overflow risk
3. Complex syntax
4. Manual memory handling
➡ These issues are solved by string class.
PART-B: string Class (C++ Strings)
2. string Class in C++
The string class is part of C++ Standard Library that provides a safe, flexible, and easy way to handle strings.
Header File
#include <string>
Declaration and Initialization
string name;
string city = "Delhi";
Input and Output Using string
Using cin
string name;
cin >> name; // Reads single word
Using getline()
string name;
getline(cin, name); // Reads full sentence
Common String Operations
1. Length of String
string s = "Hello";
cout << s.length();
2. Concatenation
string a = "Hello";
string b = "World";
cout << a + " " + b;
3. Comparison
if (a == b)
cout << "Equal";
4. Accessing Characters
cout << s[0]; // H
5. Modify String
s[0] = 'h';
Common String Functions (string class)
| Function | Purpose |
|---|---|
length() |
Returns string length |
size() |
Same as length |
append() |
Adds text at end |
insert() |
Insert text at index |
erase() |
Removes portion |
replace() |
Replace substring |
find() |
Find substring index |
substr() |
Extract substring |
Example: find() and substr()
string s = "Programming";
cout << s.find("gram"); // Output: 3
cout << s.substr(3, 4); // gram
Example Program – All String Operations in One C++ Program
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello";
cout << "Original String: " << s << endl;
// 1. length() – find length of string
cout << "Length using length(): " << s.length() << endl;
// 2. size() – same as length
cout << "Length using size(): " << s.size() << endl;
// 3. append() – add string at the end
s.append(" World");
cout << "After append(): " << s << endl;
// 4. insert() – insert text at specific position
s.insert(5, ",");
cout << "After insert(): " << s << endl;
// 5. erase() – remove part of string
s.erase(5, 1); // remove comma
cout << "After erase(): " << s << endl;
// 6. replace() – replace substring
s.replace(6, 5, "C++");
cout << "After replace(): " << s << endl;
// 7. find() – find substring position
int pos = s.find("C++");
if (pos != string::npos) {
cout << "'C++' found at position: " << pos << endl;
}
// 8. substr() – extract substring
string sub = s.substr(6, 3);
cout << "Substring using substr(): " << sub << endl;
return 0;
}
Output (Expected)
Original String: Hello
Length using length(): 5
Length using size(): 5
After append(): Hello World
After insert(): Hello, World
After erase(): Hello World
After replace(): Hello C++
'C++' found at position: 6
Substring using substr(): C++
Real-World Applications of Strings
-
Login systems
-
Searching text
-
Chat applications
-
File handling
-
Web development
-
Data validation
Difference Between C-Style String and String Class
| Feature | C-Style | string class |
|---|---|---|
| Null character | Required | Not needed |
| Size | Fixed | Dynamic |
| Safety | Low | High |
| Ease of use | Hard | Easy |
| Recommended | ❌ | ✅ |
Q1. What is a string?
✔ A sequence of characters used to represent text.
Q2. Difference between char array and string?
✔ Char array is fixed and unsafe, string is dynamic and safe.
Q3. Which header file is used for strings?
✔ <string>
Q4. What is null character?
✔ '\0' marks end of C-string.
Q5. What is getline()?
✔ Reads an entire line including spaces.
Best Practices
1. Prefer string class over char array
2. Use getline() for sentences
3. Pass strings by reference for efficiency
4. Avoid hard-coded sizes
Conclusion
Strings are one of the most important topics in C++ programming. From simple input handling to complex text processing, strings are everywhere. Mastering C++ string class makes your programs safer, cleaner, and more professional.
Keep practicing — you're doing amazing!
Happy Coding! ![]()