Palindrome in C++
This program tells weather the word is palindrome or not and reverses the word given by the user with a number of letters.
#include
<iostream>
#include
<cstring> ##To use string library
using namespace
std;
int main() {
string word;
int i;
int points=0;
cout << "Enter the
palindrome word(small letter) you want check: ";
getline(cin, word); #Taking the input
from the user
int a = word.length(); ##Counting
numbers of words and storing in integer a
cout << "The word is "
<< a << " letters long." << endl;
cout << "The reverse word is
";
{ ##Use of reverse iterator to
reverse the word
for (std::string::reverse_iterator
rit = word.rbegin(); rit != word.rend(); ++rit)
cout << *rit;
}
cout << ". \n";
for (i = 0; i <= a; i++){ ##Using for loop to compare word
if(word[i]!=word[a-i-1]) {
points = 1; ##if true
break;
}
}
if (points) { ##Incase of true
cout << word << " is
not a palindrome" << endl;
}
else { ##Incase of false
cout << word << " is a
palindrome" << endl;
}
system("pause");
return 0;
}
To learn more about strings in C++: http://www.cplusplus.com/reference/string/string/
Comments
Post a Comment