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: Mumbai

String 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: 11

Accessing 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 Morning

Common String Functions

FunctionDescriptionExample
.length()Returns string lengths.length()
.size()Same as length()s.size()
.empty()Returns true if string is emptys.empty()
.substr(pos, len)Extract part of strings.substr(0, 3)
.find(str)Find position of substrings.find("lo")
.replace(pos, len, str)Replace part of strings.replace(0, 3, "Hi")
.erase(pos, len)Remove part of strings.erase(2, 3)
.append(str)Append to end of strings.append("!")
.compare(str)Compare two stringss.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:

World

Finding 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: 7

Converting 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:

Different

Iterating Over a String

string name = "Code";
for (char c : name) {
    cout << c << "-";
}

Output:

C-o-d-e-

Key Takeaways

  • Use the string class 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(), and replace() make string manipulation easy.
  • Use stoi() and to_string() to convert between strings and numbers.

Leave a Comment

Your email address will not be published. Required fields are marked *