HOWTO · C++

How to Get the Last Character From a String in C++

This article talks about getting the last character from a string in C++.

On this page

For a string such as "Spider Man", text.back() directly refers to its final character. You can also use at(text.size() - 1) when you want checked positional access. The string must contain a character before any of these approaches is used.

Use std::string::at to Get the Last Character From a String in C++

std::string::at(index) returns the character at a zero-based position and checks that the position is within the string. The final position is text.size() - 1. If the index is outside the string, at() throws std::out_of_range; the index type is unsigned, so a negative value converts to a large unsigned value rather than acting as a valid negative index.

Run this example:

#include <iostream>
#include <string>

int main() {
    const std::string text = "Spider Man";
    std::cout << "Character at position 3: " << text.at(3) << '\n';
    std::cout << "Character at last position: "
              << text.at(text.size() - 1) << '\n';
}

The first access uses index 3; the second computes the final index from the string length. Check that the string is not empty before computing and using that final position.

Use std::string::back() to Get the Last Character From a String in C++

std::string::back() is the direct way to access the final character. It returns a reference, so you can read the character or assign a replacement through that reference. Calling back() on an empty string has undefined behavior, so check the string before using it.

#include <iostream>
#include <string>

int main() {
    std::string text = "Spider Man";
    const char last = text.back();
    std::cout << "Character at the end: " << last << '\n';
    text.back() = '@';
    std::cout << "After replacement: " << text << '\n';
}

The first call reads n; the assignment changes the string to "Spider Ma@".

Use std::string::rbegin() to Get the Last Character From a String in C++

std::string::rbegin() returns a reverse iterator whose first position corresponds to the string’s final character. Dereference it to read that character, provided the string is not empty.

#include <iostream>
#include <string>

int main() {
    const std::string text = "Spider Man";
    const auto last = *text.rbegin();
    std::cout << "The last character is: " << last << '\n';
}

Use rbegin() when working with reverse iteration; for a single character, back() is more direct.

Use std::reverse With String Iterators to Get the Last Character in C++

std::reverse is an algorithm declared in <algorithm>, not a std::string member function. It reverses the elements in the iterator range in place. After reversing a nonempty string, text.front() refers to the character that was originally last. This changes the entire string, so it is usually unnecessary when you only need to read its final character.

#include <algorithm>
#include <iostream>
#include <string>

int main() {
    std::string text = "Spider Man";
    std::reverse(text.begin(), text.end());
    std::cout << "The former last character is: " << text.front() << '\n';
}

The algorithm operates on the half-open range from begin() to end(). Make sure the string is not empty before reading front() afterward.

Use std::string::substr() to Get the Last Character From a String in C++

std::string::substr(position, count) returns a new string. For a string with at least one character, text.substr(text.size() - 1, 1) extracts its final character as a one-character string. Check for a character before calculating the starting position.

#include <iostream>
#include <string>

int main() {
    const std::string text = "Spider Man";
    const std::string last = text.substr(text.size() - 1, 1);
    std::cout << "Last character extracted: " << last << '\n';
}

Choose substr() when the result should be a string; use a character access operation when a char is sufficient.

Use Array Indexing to Access the Characters of a String in C++

std::string supports operator[], so the final character can be read as text[text.size() - 1] after confirming the string is not empty. Unlike at(), operator[] does not provide the same checked access for an out-of-range position.

#include <iostream>
#include <string>

int main() {
    const std::string text = "Spider Man";
    std::cout << "The last character is: "
              << text[text.size() - 1] << '\n';
}

The index is zero-based: for a string of length n, the final character is at index n - 1. Prefer at() if you want an out-of-range index to be reported by an exception.

Conclusion

For direct access to the final character of a nonempty std::string, use back(). Choose at(size() - 1) when checked positional access is useful. rbegin() also reaches the final character through reverse iteration, substr() returns it as a string, and indexing uses the same final position without at()’s bounds check. Check that the string contains a character before accessing its end.