Advertisement
Syndragonic

Palindrome Testing CS311

Aug 25th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. using namespace std;
  5. //Function that swaps values of v1 and v2
  6. void swap (char& v1, char& v2);
  7. //Function that returns a copy of s but in a reversed order
  8. string reverse (const string& s);
  9. //Function that returns s without the punctuations
  10. string removePunct (const string& s, const string& punct);
  11. //Making the the string s to lowercase
  12. string makeLower (const string& s);
  13. //Function for True or False if it is a palindrome or not
  14. bool isPal (const string& s);
  15.  
  16. void swap (char& v1, char& v2)
  17. {
  18. char temp = v1;
  19. v1 = v2;
  20. v2 = temp;
  21. }
  22.  
  23. string reverse(const string& s)
  24. {
  25. int start = 0;
  26. int end = s.length();
  27. string temp(s);
  28.  
  29. while (start < end)
  30. {
  31. end--;
  32. swap(temp[start], temp[end]);
  33. start++;
  34. }
  35. return temp;
  36. }
  37. string makeLower(const string& s)
  38. {
  39. string temp(s);
  40. for (int i = 0; i < s.length(); i++)
  41. temp[i] = tolower(s[i]);
  42. return temp;
  43. }
  44. string removePunct (const string& s, const string& punct)
  45. {
  46. string noPunct;
  47. int sLength = s.length();
  48. int punctLength = punct.length();
  49. for (int i = 0; i < sLength; i++)
  50. {
  51. string aChar = s.substr(i,1);
  52. int location = punct.find(aChar, 0);
  53.  
  54. if (location < 0 || location >= punctLength)
  55. noPunct = noPunct + aChar;
  56. }
  57. return noPunct;
  58. }
  59. bool isPal(const string& s)
  60. {
  61. string punct(",;:.?!'\" ");
  62. string str(s);
  63. str = makeLower(str);
  64. string lowerStr = removePunct(str, punct);
  65.  
  66. return (lowerStr == reverse(lowerStr));
  67. }
  68.  
  69. int main()
  70. {
  71. string str;
  72. cout<< "Enter a candidate for palindrome test\n"<<"followed by pressing Return.\n";
  73. getline(cin, str);
  74.  
  75. if (isPal(str))
  76. cout<< "\"" <<str + "\" is a palindrome.";
  77. else
  78. cout<< "\"" <<str + "\" is not a palindrome.";
  79. cout<< endl;
  80.  
  81. return 0;
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement