Program in C++ to check a given string is palindrome or not Using function
First Method
#include<iostream> #include<string.h> using namespace std; void isPalin(char str[]) { int len,c=0; len=strlen(str); for(int i=0;i<len/2;i++) { if(str[i]==str[len-1-i]) c++; else { cout<<str<<" is not palindrome string"; return; } } if(len/2==c) cout<<str<<" is palindrome string"; } int main() { char str1[30]; cout<<"Enter a string:-"; cin>>str1; isPalin(str1); return 0; }
Output:-
Second Method
#include<iostream> #include<string.h> using namespace std; void isPalin(char str[]) { int len,i,j; len=strlen(str); char rev[len]; for(j=0,i=len-1;i>=0;i--,j++) { rev[i]=str[j]; } if(strcmp(str,rev)==0) //if(strcmp(str,rev)==0) cout<<str<<" is palindrome string"; else cout<<str<<" is not palindrome string"; } int main() { char str1[30]; cout<<"Enter a string:-"; cin>>str1; isPalin(str1); return 0; }
Output:-
0 Comments