Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. bool testPal(const string& s)
  7. {
  8. //tests for palindrome
  9. string frontToBack(s.rbegin(), s.rend());
  10. return s == frontToBack;
  11. }
  12.  
  13. string removeSpace(string line)
  14. // removes whitespace for phrases
  15. {
  16. while (1)
  17. {
  18. int word = line.find(' ');
  19. if (word == -1 )
  20. {
  21. break;
  22. }
  23. line = line.erase(word, 1);
  24. }
  25. return line;
  26. }
  27.  
  28. int main()
  29. {
  30. // Inititizations
  31. string word, word2;
  32. char yesno = 'Y', wordPhrase = 'W';
  33. // welcome message
  34. cout << "\nWelcome to the palindrome program.\n"
  35. << "Are you testing for Word or Phrase palindromes?(Word is default)\n";
  36. cin >> wordPhrase;
  37. cin.ignore(INT_MAX, '\n');
  38. wordPhrase = toupper(wordPhrase);
  39. //outputs either word or phrase depending on selection
  40. //defaults to word
  41. switch(wordPhrase)
  42. {
  43. case 'W':
  44. cout << "Word option selected.\n";
  45. break;
  46. case 'P':
  47. cout << "Phrase option selected.\n";
  48. break;
  49. default:
  50. cerr << "Invalid input. Reverting to word.";
  51. wordPhrase = 'W';
  52. break;
  53. }
  54. // loop until broken by yesno
  55. while ('Y' == yesno)
  56. {
  57. for(int i = 0; i < 1 ; i++)
  58. {
  59. cout << "\nWhat is your ";
  60. //outputs word or phrase depending on input to make
  61. //it more user friendly
  62. if (wordPhrase == 'W')
  63. {
  64. cout << "word?\n";
  65. }
  66. if (wordPhrase == 'P')
  67. {
  68. cout << "phrase?\n";
  69. }
  70. getline(cin, word);
  71. word2 = word;
  72. if ('P' == wordPhrase)
  73. {
  74. word = removeSpace(word);
  75. }
  76. // output depending on isPalindrome function
  77. if (testPal(word))
  78. {
  79. cout << "\n" << word2 << " is a palindrome.";
  80. }
  81. else
  82. {
  83. cout << "\n" << word2 << " isn't a palindrome.";
  84. }
  85. cout << "\n\nAgain?(Y/N)\n";
  86. cin >> yesno;
  87. cin.ignore(INT_MAX, '\n');
  88. yesno = toupper(yesno);
  89. }
  90. }
  91. // exit message
  92. cout << "\nHave a nice day. Exiting...\n";
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement