0

palindrom

i want my program take a word from user then determine if it is palindrom or not, what is the best way .i write two programs which both of them could not take from user. i found this on the net, but it also does not work. help me to run this. thanks in advance. #include<iostream> #include<string> using namespace std; int main( ) { char str[80]; cout<<"Enter string: "; cin.getline(str, 80); int l; //Hold length of string //finding length of string for(l = 0; str[l] != '\0'; l++); //Comparing first element with last element till middle of string int i; for(i = 0; (i < l/2) && (str[i] == str[l - i - 1]); i++); if(i == l/2) cout << "Palindrome"; else cout << "Not a palindrome"; return 0; } }

28th Nov 2016, 9:27 PM
parisa
parisa - avatar
3 Réponses
+ 3
hi you can use this simple code #include <iostream> #include <string> using namespace std; int main() { string str; cin>>str; int n=str.length(); bool isPalindrome=true; for(int i=0, j=n-1;i<n;i++,j--){ if(str[i]!=str[j]){ isPalindrome=false; break; } } if(isPalindrome){ cout<<str<<" is palindrome"<<endl; }else{ cout<<str<<" is not palindrome"<<endl; } return 0; }
28th Nov 2016, 11:22 PM
Saeed
Saeed - avatar
+ 3
i think its problem for getline or char array and why did you use char array? why did you dont use string and get length string simply by length() or sizeof() function when you dont need to use loop for get length
29th Nov 2016, 4:03 PM
Saeed
Saeed - avatar
0
thanks a lot. but can you tell me what is problem of my program?
29th Nov 2016, 6:10 AM
parisa
parisa - avatar