Open Laboratory for Technocrats

Grab the developer role, learn concepts & prepare with senior software engineers to get solutions for problems in a software job. Coding and Programming Solutions crafted for you.

How do I write a C++ program to check whether the string is palindrome or not without using inbuilt functions?

Palindrome.jpg
Palindrome.jpg
What is Palindrome?
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:

#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;
}
Kepp Coding and Kepp Learning :)

Top #3 Articles

Most Read