Advertisement
Hollowfires

Untitled

Dec 9th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 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. #include <stdbool.h>
  21. #include <cctype>
  22.  
  23. using namespace std;
  24. //prototype
  25. bool isPalindrome(string, 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. int size=64;
  32. char inputStr[size];
  33.  
  34. cout << "Please input a word to be verified whether or not it is a palindrome: " << endl;
  35. cin.getline(inputStr, size);
  36.  
  37. int beginning = 0;
  38. int ending = strlen(inputStr) -1;
  39.  
  40.  
  41. int returned;
  42.  
  43. returned = isPalindrome(inputStr, beginning, ending);
  44.  
  45. if (returned == 1)
  46. cout << inputStr << " is a palindrome. "<<endl;
  47. else
  48. cout << inputStr << " isn't a palindrome. "<<endl;
  49.  
  50.  
  51. return 0;
  52.  
  53. }
  54.  
  55.  
  56. bool isPalindrome(string inputStr, int b, int e)
  57. {
  58. //if you only put 1 letter i
  59. if (b == e)
  60. return true;
  61.  
  62. //if the first and last characters don't match then it's not a palindrome
  63. if (inputStr[b] != inputStr[e])
  64. return false;
  65.  
  66. //If there are more than 1 or 2 characters
  67. //then the function will continue to call itself.
  68. //until it finally reaches the middle term.
  69. if (b < e + 1)
  70. return isPalindrome(inputStr, b + 1,e - 1);
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement