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.
Palindromic numbers are numbers that remain the same when their digits are reversed, such as 121 or 73837.
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.
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;
}
We are comparing each ith character to ith last character. If they are equal then we proceed forward else we return false.
Top comments (0)