So basically this is a palindrome checker for strings. The problem is that I have to make it run 3 times, so a for loop, but then the "Not a palindrome" goes in an infinite loop. Could someone help me with this problem thanks.
#include <iostream>
using namespace std;
int main() {
int x, y;
long z;
string word;
cout<<"Enter a word or sentence: ";
getline(cin,word);
z = word.length();
for (int i = 0; i < z; i++) {
if(word[i] == ' ')
word.erase(i, 1);
}
Hi @wawaluigi,
From what I've seen in your code, you wrote that "if" statement inside the loop, and did not make it stop until you reach the middle of the string.
You could try doing this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
while (x < z/2)
{
if(word[x] == word[y])
{
x++;
y-=1;
}
else
{
cout<<"Not a palindrome"<<endl;
//added line
break; //this stops the loop when you know it's not a palindrome.
}
}