Hollowfires

Untitled

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