Strings

std::string: length, character access, traversal and a palindrome check.

A string is a sequence of characters. In C++ strings are stored in the std::string type, and characters are accessed by index just like in an array: s[0] is the first character, s[s.size() - 1] is the last. The s.size() method returns the length of the string. Strings can be compared with == and <, concatenated with + and traversed with a loop.
C++

Enter a word, for example: hello

The program above prints the word backwards — by traversing indices from the end. The classic string problem is the palindrome: does a word read the same left to right and right to left? The idea: compare the first character with the last, the second with the second-to-last, and so on up to the middle.
C++

Palindrome check. Enter: racecar

Task: read a word and count how many letters "a" it contains. Three problems are attached to this lesson: "Reverse a String", "Palindrome Check" and "Count the Vowels". Solve them all — it is the best way to cement string handling — and mark the lesson as completed.

Practice problems

Доска