Last symbol#11152

Last symbol (Editorial)

Let be the input string. Its last character can be accessed using the back() method.

The problem can also be solved by calculating the index of the last character of the string. It is i = s.size() - 1, and the character itself can be accessed as .

Algorithm implementation

Read the input string. Print the last character.

getline(cin, s);
cout << s.back() << endl;

Algorithm implementation — index of the last character

Read the input string.

getline(cin, s);

Compute the position of the last character.

i = s.size() - 1;

Print the last character.

cout << s[i] << endl;

Python implementation

Read the input string.

s = input()

Print the last character. Use negative indexing.

print(s[-1])