![]() |
Palindrome.jpg |
According to Wikipedia:
A palindrome is a word, number, phrase, or other sequences of characters which reads the same backward as forward, such as madam or racecar or the number 10801. Sentence-length palindromes may be written when allowances are made for adjustments to capital letters, punctuation, and word dividers, such as "A man, a plan, a canal, Panama!", "Was it a car or a cat I saw?" or "No 'x' in Nixon".Let's watch a video which explains the concept of this :
Now, let's jump into the code that will implement this in C++ Langauge:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
int calculateLength(char *input){ | |
int i = 0; | |
while (input[i]){ | |
i++; | |
} | |
return i; | |
} | |
int main(){ | |
char input[100]; //Getting Input from User upto 100 chars | |
std::cout << "Please input the string/number [Note: Max 100 characters allowed]:"; | |
std::cin >> input; | |
int length = calculateLength(input); | |
int lastIndex = length - 1; | |
bool isPalindrome = true; | |
for (int i = lastIndex; i >= length / 2; i--){ | |
if (input[i] != input[lastIndex - i]){ | |
isPalindrome = false; | |
break; | |
} | |
} | |
if (isPalindrome){ | |
std::cout << ":) Its a Palindrome" << std::endl; | |
}else{ | |
std::cout << ":( Its Not palindrome" << std::endl; | |
} | |
return 0; | |
} |