48 lines
822 B
C++
48 lines
822 B
C++
|
#include <iostream>
|
||
|
#include <string>
|
||
|
using namespace std;
|
||
|
|
||
|
int main(){
|
||
|
|
||
|
string str;
|
||
|
cout << "Enter a palindrome: " << endl;
|
||
|
getline(cin,str);
|
||
|
|
||
|
//test our use of getline
|
||
|
//cout << str << endl;
|
||
|
//a man a plan a canal panama
|
||
|
//goal: test whether a phrase is a palindrome
|
||
|
//in just one while loop
|
||
|
int l=str.length();
|
||
|
int i=0; // counting from the left
|
||
|
int j=l-1; // counting from the right
|
||
|
|
||
|
bool isPal = true;
|
||
|
while(i < l){
|
||
|
if(str[i] != ' ' && str[j] != ' '){
|
||
|
if(str[i] != str[j]){
|
||
|
isPal = false;
|
||
|
}
|
||
|
i++;
|
||
|
j--;
|
||
|
}
|
||
|
else {
|
||
|
if(str[i] == ' '){
|
||
|
i++;
|
||
|
}
|
||
|
|
||
|
if(str[j] == ' '){
|
||
|
j--;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(isPal){
|
||
|
cout << "is a palindrome" << endl;
|
||
|
}
|
||
|
else{
|
||
|
cout << "ain't a palindrome" << endl;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|