How to check if a string is a palindrome in C++ 20

A palindrome is a sequence of characters, such as a word, phrase, or number, that reads the same forwards as it does backward. In other words, it remains unchanged when its order is reversed. This property frequently gives rise to captivating patterns, making palindromes a subject of significant interest in programming challenges, algorithmic exploration, and string manipulation tasks.

Example

Palindromes are prevalent in both words and numbers. In words, palindromes like radar, level, and rotator exhibit symmetry when read forwards or backward. Numeric palindromes such as 1221 and 1331 remain unchanged when their digits are reversed.

Example of palindrome
Example of palindrome

The <ranges> library in C++ 20

The addition of the <ranges> library is a notable enhancement in C++20. The <ranges> library extends and enhances the capabilities of the algorithms and iterator libraries. This is achieved by introducing composability and reducing the potential for errors. Through this library, range views are crafted and managed as lightweight entities that indirectly stand for iterable sequences or ranges.

Within the ranges library, we’ll find both range algorithms, which promptly operate on ranges, and range adaptors, which are employed on views in a deferred manner. These adaptors can be pieced together into pipelines, allowing their functions to be executed as the view is traversed. We can use the <ranges> library to reverse a sequence and check if it’s a palindrome.

The std::string_view class

The std::string_view class provides a non-owning view of a sequence of characters. It’s useful for efficiently working with strings without copying them. We can use std::string_view to represent a word being checked for being a palindrome.

Code example

#include <iostream>
#include <string>
#include <algorithm>
#include <ranges>
#include <string_view>

using namespace std;
bool is_palindrome(string_view str) {
    return ranges::equal(str, str | views::reverse);
}

int main() {
    string_view word = "racecar";

    if (is_palindrome(word)) {
        cout << "The given string is a palindrome." << endl;
    } else {
        cout << "The given string is not a palindrome." << endl;
    }

    return 0;
}
Checking if a string is a palindrome using range library

Code explanation

  • Lines 1–5: Include header files that are necessary for the code execution.

  • Lines 8–9: The is_palindrome function takes a string_view called str as an argument and returns a boolean bool. A string_view is an efficient way to work with a sequence of characters without owning the memory containing those characters. The is_palindrome function uses the ranges::equal function from the ranges namespace to check if the given str is a palindrome. It does so by comparing the original string with its reverse using the | (pipe) operator in combination with the views::reverse view.

  • Line 13: A string_view named word is created and initialized with the string "racecar".

  • Lines 15–19: This block of code checks if the word is a palindrome by calling the is_palindrome function. If it’s a palindrome, it prints "The given string is a palindrome." to the console .Otherwise, it prints "The given string is not a palindrome."

  • Line 21: The return 0; statement signals that the program was executed successfully.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved