Get the Last Character From a String in C++

Suraj P Feb 03, 2023 Mar 22, 2022
  1. Use std::string::at to Get the Last Character From a String in C++
  2. Use std::string::back() to Get the Last Character From a String in C++
  3. Use std::string::rbegin() to Get the Last Character From a String in C++
  4. Use std::string:begin() and reverse() to Get the Last Character From a String in C++
  5. Use Array Indexing to Access the Characters of a String in C++
Get the Last Character From a String in C++

This article will teach different methods to get the last character from a string in C++.

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

We can use std::string::at to extract a character present at a given position from a string.

Syntax:

char& string::at(size_type indexNo)

This returns the character at a specified position at the indexNo specified by the user. The method throws an out_of_range exception when an invalid index number is passed, like an index less than zero or an index greater than or equal to string.size().

Example: Extracting the last character and the character at a given position.

#include <iostream>
using namespace std;

int main()
{
    string str("Spider Man");

    cout<<"Character at position 3: "<<str.at(3)<<endl;

    int length = str.size();

    cout<<"Character at last position: "<<str.at(length-1)<<endl;

}
Explanation
To get the character at the last position, we first find the length of the string and pass length-1 in the std::string::at() method.

Output:

Character at position 3: d
Character at last position: n

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

std::string::back() in C++ gives the reference to the last character of string. This works only on non-empty strings.

This function can also replace the character at the end of the string.

Syntax:

  1. To access the last character.

    char ch = string_name.back();
    
  2. To replace the character present at the end of the string.

    str.back() = 'new_char_we_to_replace_with';
    

This function takes no parameter, and it gives the reference to the last character of the string. It shows undefined behavior when applied on an empty string.

Example code:

#include <iostream>
using namespace std;

int main()
{
    string str("Spider Man");
    char ch = str.back();

    cout<<"Character at the end "<<ch<<endl;

    str.back() = '@';

    cout<<"String after replacing the last character: "<<str<<endl;

}

Output:

Character at the end n
String after replacing the last character: Spider Ma@

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

std::string::rbegin() in C++ stands for reverse beginning. This function is used to point to the last character of the string.

Syntax:

reverse_iterator it = string_name.rbegin();

This function does not contain any parameter, and it returns the reverse iterator pointing to the last character of the string.

Example code:

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

int main()
{
    string str("Spider Man");

    string::reverse_iterator it = str.rbegin();

    cout<<"The last character of the string is: "<<*it;

}

Output:

The last character of the string is: n

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

std::string::begin() in C++ returns the iterator to the beginning of the string, taking no parameter.

Syntax:

string::iterator it = string_name.begin();

std::string::reverse() is a built-in function in C++ to directly reverse a string. It takes begin and end iterators as arguments.

This function is defined in the algorithm header file.

Syntax:

reverse(string_name.begin(), string_name.end());

To get the last character from the string, we first reverse the string using the reverse method and then use the begin() method to get the reference of the string’s first character (which was the last character before reversal) and print it.

Example code:

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

int main()
{
    string str("Spider Man");

    reverse(str.begin(),str.end());

    string::iterator it = str.begin();

    cout<<"The last character of the string is: "<<*it;

}

Output:

The last character of the string is: n

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

We can access the characters of a string by referring to it using its index number and passing it inside square brackets [].

Example code:

#include <iostream>

using namespace std;

int main()
{
    string str("Bruce banner");

    int length = str.size();

    cout<<"The last character of the string is "<<str[length-1];

}

To get the last character, we first find the length of the string and pass length-1 in the square brackets.

Output:

The last character of the string is r
Author: Suraj P
Suraj P avatar Suraj P avatar

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

LinkedIn GitHub

Related Article - C++ String