Aral Code

Basics Lesson 6 of 17

C++ Strings — std::string

Work with text using std::string — join with +, compare with ==, get length, access characters, substr, find, replace, insert, erase, and convert between strings and numbers.

C++'s std::string is a huge improvement over C's character arrays. It grows automatically, knows its own length, and can be joined with + and compared with ==. Include <string> to use it.

Basics

#include <iostream>
#include <string>
using namespace std;

int main() {
    string first = "Andres";
    string last = "Bonifacio";
    string full = first + " " + last;

    cout << full << endl;
    cout << "Length: " << full.length() << endl;
    cout << "First letter: " << full[0] << endl;
    cout << "Last letter: " << full.back() << endl;

    full += "!";
    cout << full << endl;
    return 0;
}

Output

Andres Bonifacio
Length: 16
First letter: A
Last letter: o
Andres Bonifacio!

Comparing strings

#include <iostream>
#include <string>
using namespace std;

int main() {
    string answer = "yes";
    if (answer == "yes") cout << "Proceeding..." << endl;
    cout << boolalpha << (string("apple") < string("banana")) << endl;
    cout << (string("Zebra") < string("apple")) << endl;
    return 0;
}

Output

Proceeding...
true
true

==, !=, < and > work with strings. Comparison is case-sensitive and by character code, so uppercase letters come before lowercase ones: "Zebra" sorts before "apple".

Useful methods

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s = "Grade 10 - Rizal";
    cout << s.substr(0, 8) << endl;          // from index 0, 8 characters
    cout << s.substr(11) << endl;            // from index 11 to the end
    cout << s.find("Rizal") << endl;         // position
    cout << (s.find("Luna") == string::npos ? "not found" : "found") << endl;

    s.replace(6, 2, "11");                   // replace 2 chars at index 6
    cout << s << endl;
    s.insert(0, ">> ");
    cout << s << endl;
    s.erase(0, 3);
    cout << s << endl;
    cout << boolalpha << s.empty() << endl;
    return 0;
}

Output

Grade 10
Rizal
11
not found
Grade 11 - Rizal
>> Grade 11 - Rizal
Grade 11 - Rizal
false
MethodDoes
length() / size()Number of characters
s[i], front(), back()Access characters
substr(pos, len)Part of the string
find(x)Position of x, or string::npos if not found
replace(pos, len, x)Replace a part
insert(pos, x), erase(pos, len)Add / remove characters
empty()true if there are no characters

Changing case

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main() {
    string name = "maria clara";
    for (char &c : name) {
        c = toupper(static_cast<unsigned char>(c));
    }
    cout << name << endl;
    return 0;
}

Output

MARIA CLARA

for (char &c : name) loops through each character by reference (&), so changing c changes the string itself. References get their own lesson.

Strings and numbers

#include <iostream>
#include <string>
using namespace std;

int main() {
    int qty = stoi("12");
    double price = stod("8.50");
    string receipt = "Total: " + to_string(qty * price);
    cout << qty + 1 << endl;
    cout << receipt << endl;
    return 0;
}

Output

13
Total: 102.000000

stoi (string to int), stod (string to double) and to_string. Note that to_string on a double always shows 6 decimals; use ostringstream or format (C++20) for nicer formatting.

Counting vowels

#include <iostream>
#include <string>
using namespace std;

int main() {
    string word = "Pilipinas";
    int vowels = 0;
    for (char c : word) {
        if (string("aeiouAEIOU").find(c) != string::npos) vowels++;
    }
    cout << word << " has " << vowels << " vowels." << endl;
    return 0;
}

Output

Pilipinas has 4 vowels.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Can you compare C++ strings with ==?
2. What does string("Pilipinas").substr(2, 3) return?
3. What does find return when the text isn't found?
4. Which function converts "42" to an int?

By Sir Reyel · Last updated 2026-12-04