1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
bool check_palindrome(string, int, int&); // input are words, word count, and output is palindrome count
void format(string&, int, int&); // formats the words to capitalize all letters and get rid of space
int main()
{
string words;
int wcount; // word count
int pcount; // palindrome count
int n;
getline(cin,words); // gets the whole word per line
while (cin)
{
format(words,n,wcount);
if(check_palindrome(words,n,pcount))
{
cout << words << " is a palindrome" << endl;
}
else
cout << words << " is not a palindrome" << endl;
getline(cin,words);
}
}
void format(string& word, int n, int& wcount)
{
wcount = 0;
n = word.length();
for (int i = 0; i < n; i++)
{
word[i] = toupper(word[i]); // makes all letters capital
if(word[i] == ' ') // gets rid of spaces between letters
word[i] = word[i-1];
wcount++;
}
}
bool check_palindrome(string word, int n, int& pcount)
{
pcount = 0;
n = word.length();
for (int i = 0; i < n; i++) // starts at first position of the word and goes up by 1
{
for (int j = n; j >= 0; j--) // starts at end position of word and decreases by 1
if (word[i] == word[j]) // so this should compare word[0] == word[endposition]
{
return true;
pcount ++;
}
}
return false;
}
|