Program: Find given string is palindrome or not
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.
So in this program we take input string from user and return the result whether its palindrome or not.
#include <iostream>
using namespace std;
int main() {
char str[100]; // declare char variable of length 100 i.e. max 100 character string can be taken input
cin >> str; //take input
int strLength = 0; //initialize the length of string to zero
bool palindrome = true; //let assume given string is palindrome
for(int i=0;str[i]!='\0';i++,strLength++);
/*
to understand above statement first remember array of char ends with '\0'. '\0' represents null character. So we can determine the end of string '\0' to calculate the length of string. So here we first initialize the length by 0 then we begin the loop from i =0 and we check character at ith position for null character '\0', so we are increment string length with the value of i. So when we encounter '\0' it means end of string so length is calculate with the value of i.
*/
for(int i=strLength-1,j=0;j<(strLength/2)+1;i--,j++){
if(str[i] != str[j]){
palindrome = false;
break;
}
}
/*
Now to check palindrome we check starting half of string should be match with half from end. For even length it will divide perfect in two parts whereas for odd it will left with one character that we don't need to check as its in middle and common.
Now we declare two counter first denote the position of start of string and other end of string[Remember index start from 0 so here strLength is total length so last Index will be strLength -1 ]. So we run the loop upto half length of string length and we increment j(start counter) and decrement i(end counter).
In each loop pass we check character of ith position and jth position to be same. If they mismatch in any one of the case then we can says given string is not a palindrome. So by default we assume its palindrome if condition doesn't match mean its palindrome.
*/
if(palindrome){ //print the output
cout << "Given String is Palindrome";
}
else{
cout << "Given String is not a Palindrome";
}
return 0;
}
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.
So in this program we take input string from user and return the result whether its palindrome or not.
#include <iostream>
using namespace std;
int main() {
char str[100]; // declare char variable of length 100 i.e. max 100 character string can be taken input
cin >> str; //take input
int strLength = 0; //initialize the length of string to zero
bool palindrome = true; //let assume given string is palindrome
for(int i=0;str[i]!='\0';i++,strLength++);
/*
to understand above statement first remember array of char ends with '\0'. '\0' represents null character. So we can determine the end of string '\0' to calculate the length of string. So here we first initialize the length by 0 then we begin the loop from i =0 and we check character at ith position for null character '\0', so we are increment string length with the value of i. So when we encounter '\0' it means end of string so length is calculate with the value of i.
*/
for(int i=strLength-1,j=0;j<(strLength/2)+1;i--,j++){
if(str[i] != str[j]){
palindrome = false;
break;
}
}
/*
Now to check palindrome we check starting half of string should be match with half from end. For even length it will divide perfect in two parts whereas for odd it will left with one character that we don't need to check as its in middle and common.
Now we declare two counter first denote the position of start of string and other end of string[Remember index start from 0 so here strLength is total length so last Index will be strLength -1 ]. So we run the loop upto half length of string length and we increment j(start counter) and decrement i(end counter).
In each loop pass we check character of ith position and jth position to be same. If they mismatch in any one of the case then we can says given string is not a palindrome. So by default we assume its palindrome if condition doesn't match mean its palindrome.
*/
if(palindrome){ //print the output
cout << "Given String is Palindrome";
}
else{
cout << "Given String is not a Palindrome";
}
return 0;
}
good post
ReplyDelete