I am a student and I'm doing a project with C++. Actually my program should tell the user if the word given is a palindrome or not. However, it's always saying "NO PALINDROME". Need help, please! Can't find the error!
#include<iostream>
#include<string>
usingnamespace std;
int main(){
string n, a, b;
cin>>n;
int i, p=n.size(), c=1;
for (i=1; i<n.size(); (i=i+1)and(p=p-1)){
a=(n[i]);
b=(n[p]);
if (a==b){
c=c+1;
}
else {
cout<<"NO PALINDROM"<<endl;
i=n.size();
}
}
if (c==n.size()){
cout<<"YES PALINDROM"<<endl;
}
int z;
cin>>z;
}
Well, we were told that we did not have to tell the user what should be the input, but I'm OK with the other two things. Despite those facts it continues telling "NO PALINDROME" at any word.
So I found your error. You initialize i to 1 but arrays start at zero so i is looking at the second letter of your word. then secondly, n.size() returns 1 past the number of characters in your array so setting p=n.size() then b=n[p], b is equal to ' ' (a space). So to fix these errors, set i to 0 and p to n.size()-1.
OOOH and also, look at c..... look at it.... look at it.... mmk, so you set it to one, lets go through a loop
cin: aka
a = a
b = a
c = c + 1 == 2 // Weren't we still on the first letter? yeah, so make sure to initialize c to 0 as well