Hollowfires

Untitled

Dec 9th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. //Ch14_8 Palindrome testing
  2. //Rawlin Stone
  3.  
  4. //*make a recursive function that will take a 3 parameter argument list
  5. //*First parameter will be the string name, 2nd parameter is the starting
  6. // index of the string, and the 3rd parameter will be the last index of the string
  7. //*This function will check whether the string is a palindrome or not
  8. //*The return type of the function will be boolean type.
  9. //It will return true if the string is palindrome, otherwise return false.
  10.  
  11. /*****
  12. A palindrome is a string such as "madam", "radar", "dad", and "I" that reads the same forwards and backwards.
  13. The empty string is regarded as a palindrome. Write a recursive function bool isPalindrome(string str, int lower, int upper)
  14. that returns true IFF the string str in positions lower through upper (inclusive at both ends) is a palindrome.
  15. Test your function by writing a main function that repeatedly asks the user to enter strings terminated by the ENTER key.
  16. These strings are then tested for palindromicity. The program terminates when the user presses
  17. the ENTER key without typing any character before it.
  18. ****/
  19.  
  20. #include <iostream>
  21. #include <cstring>
  22. //need it for strlen
  23. #include <cctype>
  24.  
  25. using namespace std;
  26. //prototype
  27. bool isPalindrome(char[], int, int);
  28.  
  29.  
  30. int main()
  31. {
  32. //there aren't any palindromes larger than tattarrattat, but there are phrases. 64 should be enough.
  33. //initialize the size to 64.
  34. const int SIZE=64;
  35. char inputStr[SIZE];
  36.  
  37. do{
  38.  
  39. //output to the user
  40. cout << "Please input a word to be verified whether or not it is a palindrome: " << endl;
  41.  
  42. cin.getline(inputStr, SIZE);
  43.  
  44.  
  45. if (inputStr[0] == NULL)
  46. {
  47. return 0;
  48. }
  49.  
  50. int beginning = 0;
  51. //use strlen to find the total size of the string, then -1 because
  52. //the last term is a null operator \0
  53. int ending = strlen(inputStr) -1;
  54.  
  55. //a variable to store the value of
  56. bool returned;
  57.  
  58. //call the isPalindrome function
  59. returned = isPalindrome(inputStr, beginning, ending);
  60.  
  61.  
  62. //if returned is true
  63. if (returned == true)
  64. cout << inputStr << " is a palindrome. "<<endl;
  65. else
  66. cout << inputStr << " isn't a palindrome. "<<endl;
  67. }while (strlen(inputStr) != NULL);
  68.  
  69. return 0;
  70.  
  71. }
  72. /*************************************
  73. isPalindrome
  74. This function is a recursive function.
  75. It will search through the cstring
  76. inputStr to check that it matches
  77. at all positions forward and back.
  78. ***************************************/
  79.  
  80. bool isPalindrome(char inputStr[], int b, int e)
  81. {
  82. if (inputStr[b] == ' ') b++;
  83. if (inputStr[e] == ' ') e--;
  84.  
  85. //if you only put 1 letter i
  86. if (b >= e)
  87. return true;
  88.  
  89. //if the first and last characters don't match then it's not a palindrome
  90. if (inputStr[b] != inputStr[e])
  91. return false;
  92.  
  93.  
  94. //recursive call
  95. return isPalindrome(inputStr, b + 1,e - 1);
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment