DEV Community

Cover image for Palindromes in C++
Sajjad Ali
Sajjad Ali

Posted on

2

Palindromes in C++

What is Palindrome?

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as "madam" or "racecar". It is often used in literature and word play.

You are Palindrome

Palindromic numbers are numbers that remain the same when their digits are reversed, such as 121 or 73837.

Palindromic numbers

Palindromic dates, such as 02/02/2020 or 11/11/1111, are also a popular example of palindromes.

Palindrome can be applied on sentences, numbers, phrases or even in programming languages.

Palindrome List


Define a function in C++ that checks whether the input string is palindrome or not?

#include <iostream>
using namespace std;

bool isPalindrome(string text){
  int start = 0, end = text.length() - 1;
  while(start < end)
    if(text[start++] != text[end--])
        return false;
  return true;
}

int main() {
    std::cout << isPalindrome("madam");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

We are comparing each ith character to ith last character. If they are equal then we proceed forward else we return false.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs