C++ Strings: std::string, Operations & String Algorithms
Module 7: Strings in C++
A string is a sequence of characters used to store and manipulate text. C++ supports two types of strings:
- C-Style Strings (Character Arrays)
std::string(Standard Library String)
The std::string class is recommended because it is easier, safer, and provides many built-in functions.
1. C-Style Strings
A C-style string is an array of characters terminated with the null character (\0).
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 char name[] = "Ankit"; 8 9 cout << name; 10 11 return 0; 12}
Output
1Ankit
2. std::string
The std::string class provides powerful functions for working with text.
Example
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6int main() 7{ 8 string name = "Ankit"; 9 10 cout << name; 11 12 return 0; 13}
Output
1Ankit
String Operations
length()
Returns the number of characters in a string.
Example
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6int main() 7{ 8 string text = "Tech3Space"; 9 10 cout << text.length(); 11 12 return 0; 13}
Output
110
substr()
Returns a portion of a string.
Example
1string text = "Programming"; 2 3cout << text.substr(0, 7);
Output
1Program
find()
Searches for the first occurrence of a substring.
Example
1string text = "Welcome to C++"; 2 3cout << text.find("C++");
Output
111
erase()
Removes characters from a string.
Example
1string text = "Hello World"; 2 3text.erase(5, 6); 4 5cout << text;
Output
1Hello
replace()
Replaces part of a string.
Example
1string text = "Hello Java"; 2 3text.replace(6, 4, "C++"); 4 5cout << text;
Output
1Hello C++
append()
Adds text to the end of a string.
Example
1string first = "Tech3"; 2first.append("Space"); 3 4cout << first;
Output
1Tech3Space
String Algorithms
Palindrome
A palindrome reads the same forwards and backwards.
Example
1#include <iostream> 2#include <string> 3#include <algorithm> 4 5using namespace std; 6 7int main() 8{ 9 string text = "madam"; 10 string rev = text; 11 12 reverse(rev.begin(), rev.end()); 13 14 if (text == rev) 15 cout << "Palindrome"; 16 else 17 cout << "Not Palindrome"; 18 19 return 0; 20}
Output
1Palindrome
Reverse String
Example
1#include <iostream> 2#include <algorithm> 3 4using namespace std; 5 6int main() 7{ 8 string text = "Hello"; 9 10 reverse(text.begin(), text.end()); 11 12 cout << text; 13 14 return 0; 15}
Output
1olleH
Character Frequency
Count the occurrence of each character.
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 string text = "banana"; 8 int count = 0; 9 10 for (char ch : text) 11 { 12 if (ch == 'a') 13 count++; 14 } 15 16 cout << count; 17 18 return 0; 19}
Output
13
String Compression
String compression replaces consecutive repeated characters with the character followed by its count.
Example:
1aaabbcccc
Compressed Output
1a3b2c4
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 string str = "aaabbcccc"; 8 9 for (int i = 0; i < str.length();) 10 { 11 char current = str[i]; 12 int count = 0; 13 14 while (i < str.length() && str[i] == current) 15 { 16 count++; 17 i++; 18 } 19 20 cout << current << count; 21 } 22 23 return 0; 24}
Output
1a3b2c4
Comparison of String Functions
| Function | Purpose |
|---|---|
length() | Returns string length |
substr() | Extracts a substring |
find() | Searches for text |
erase() | Removes characters |
replace() | Replaces text |
append() | Adds text at the end |
Best Practices
- Prefer
std::stringover C-style strings. - Use built-in string functions whenever possible.
- Validate indices before using
substr()orerase(). - Use
getline()to read strings containing spaces. - Use algorithms like
reverse()from the Standard Library for cleaner code.
Practice Problems
- Find the length of a string.
- Reverse a string.
- Check whether a string is a palindrome.
- Count vowels and consonants.
- Count the frequency of each character.
- Remove duplicate characters.
- Replace one word with another.
- Compress a string.
- Find the first occurrence of a substring.
- Concatenate two strings.
Interview Questions
- What is the difference between C-style strings and
std::string? - What does the
length()function return? - How does
substr()work? - What is the purpose of
find()? - How do
erase()andreplace()differ? - What is string compression?
- How can you check if a string is a palindrome?
- How do you reverse a string in C++?
Summary
In this module, you learned:
- The difference between C-style strings and
std::string. - How to use common string operations like
length(),substr(),find(),erase(),replace(), andappend(). - How to solve common string problems such as palindrome checking, reversing a string, counting character frequency, and string compression.
- Best practices and practical exercises for mastering string manipulation in C++.
Next Module: Module 8: Pointers — Pointer Basics, Pointer Arithmetic, Pointers and Arrays, Dynamic Memory Allocation (new and delete), Null Pointers, Void Pointers, Function Pointers, Smart Pointers, and Memory Management.