Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- bool testPal(const string& s)
- {
- //tests for palindrome
- string frontToBack(s.rbegin(), s.rend());
- return s == frontToBack;
- }
- string removeSpace(string line)
- // removes whitespace for phrases
- {
- while (1)
- {
- int word = line.find(' ');
- if (word == -1 )
- {
- break;
- }
- line = line.erase(word, 1);
- }
- return line;
- }
- int main()
- {
- // Inititizations
- string word, word2;
- char yesno = 'Y', wordPhrase = 'W';
- // welcome message
- cout << "\nWelcome to the palindrome program.\n"
- << "Are you testing for Word or Phrase palindromes?(Word is default)\n";
- cin >> wordPhrase;
- cin.ignore(INT_MAX, '\n');
- wordPhrase = toupper(wordPhrase);
- //outputs either word or phrase depending on selection
- //defaults to word
- switch(wordPhrase)
- {
- case 'W':
- cout << "Word option selected.\n";
- break;
- case 'P':
- cout << "Phrase option selected.\n";
- break;
- default:
- cerr << "Invalid input. Reverting to word.";
- wordPhrase = 'W';
- break;
- }
- // loop until broken by yesno
- while ('Y' == yesno)
- {
- for(int i = 0; i < 1 ; i++)
- {
- cout << "\nWhat is your ";
- //outputs word or phrase depending on input to make
- //it more user friendly
- if (wordPhrase == 'W')
- {
- cout << "word?\n";
- }
- if (wordPhrase == 'P')
- {
- cout << "phrase?\n";
- }
- getline(cin, word);
- word2 = word;
- if ('P' == wordPhrase)
- {
- word = removeSpace(word);
- }
- // output depending on isPalindrome function
- if (testPal(word))
- {
- cout << "\n" << word2 << " is a palindrome.";
- }
- else
- {
- cout << "\n" << word2 << " isn't a palindrome.";
- }
- cout << "\n\nAgain?(Y/N)\n";
- cin >> yesno;
- cin.ignore(INT_MAX, '\n');
- yesno = toupper(yesno);
- }
- }
- // exit message
- cout << "\nHave a nice day. Exiting...\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement