Strings in C++
A string is a sequence of characters. In C++, strings can be handled in two ways: using C-style character arrays (from the C language) or using the C++ string class from the <string> header — which is much easier and more powerful.
C++ String Class
The string class is the modern and recommended way to work with text in C++. It handles memory automatically and provides many built-in functions.
#include <iostream>
#include <string>
using namespace std;
int main() {
string city = "Mumbai";
cout << "City: " << city << endl;
return 0;
}
Output:
City: MumbaiString Input — cin and getline
cin reads only up to the first space. Use getline() to read full lines including spaces.
string name;
cout << "Enter full name: ";
getline(cin, name);
cout << "Hello, " << name << "!" << endl;
Sample Run:
Enter full name: Ravi Kumar
Hello, Ravi Kumar!String Length — .length() or .size()
string word = "Programming";
cout << "Length: " << word.length() << endl;
Output:
Length: 11Accessing Individual Characters
string s = "Hello";
cout << s[0] << endl; // H
cout << s[4] << endl; // o
String Concatenation
Two strings can be joined using the + operator:
string first = "Good";
string second = " Morning";
string message = first + second;
cout << message << endl;
Output:
Good MorningCommon String Functions
| Function | Description | Example |
|---|---|---|
.length() | Returns string length | s.length() |
.size() | Same as length() | s.size() |
.empty() | Returns true if string is empty | s.empty() |
.substr(pos, len) | Extract part of string | s.substr(0, 3) |
.find(str) | Find position of substring | s.find("lo") |
.replace(pos, len, str) | Replace part of string | s.replace(0, 3, "Hi") |
.erase(pos, len) | Remove part of string | s.erase(2, 3) |
.append(str) | Append to end of string | s.append("!") |
.compare(str) | Compare two strings | s.compare("Hi") |
Substring Extraction
string text = "Hello World";
string part = text.substr(6, 5); // start at index 6, take 5 chars
cout << part << endl;
Output:
WorldFinding a Substring
string sentence = "C++ is powerful";
int pos = sentence.find("powerful");
if (pos != string::npos) {
cout << "Found at index: " << pos << endl;
} else {
cout << "Not found" << endl;
}
Output:
Found at index: 7Converting String to Number and Vice Versa
#include <string>
using namespace std;
// String to integer
string numStr = "42";
int num = stoi(numStr);
cout << num + 8 << endl; // 50
// Integer to string
int val = 100;
string s = to_string(val);
cout << s + " rupees" << endl; // "100 rupees"
Comparing Strings
string a = "apple";
string b = "banana";
if (a == b) {
cout << "Same" << endl;
} else {
cout << "Different" << endl;
}
Output:
DifferentIterating Over a String
string name = "Code";
for (char c : name) {
cout << c << "-";
}
Output:
C-o-d-e-Key Takeaways
- Use the
stringclass from<string>for easy string handling. getline()reads full lines including spaces.- Strings support concatenation with
+and comparison with==. - Built-in functions like
substr(),find(), andreplace()make string manipulation easy. - Use
stoi()andto_string()to convert between strings and numbers.
