Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. using namespace std;
  5. bool check(char word[], int index) /// function to check if palindrome or not
  6. {
  7. int len = strlen(word) - (index + 1);
  8. if (word[index] == word[len])
  9. {
  10. if (index + 1 == len || index == len){
  11. return 1; ///if string is a palindrome
  12. }
  13. check(word, index + 1); /// function calling itself
  14. }
  15. else{
  16. return 0; /// if not a palindrome
  17. }
  18. }
  19.  
  20. int main() //main function
  21. {
  22. ///declare the variables
  23. string string;
  24. bool b;
  25. char word[100];
  26.  
  27. cout <<"Welcome!" << endl;
  28. cout<<"Please enter a word: ";
  29. getline(cin , string); /// user inputs desired string to check
  30.  
  31. strcpy(word, string.c_str()); /// fill array up by reading string into it
  32. b = check(word, 0); ///function call to return a false or true return statement
  33.  
  34. if(b){
  35. cout<< string << " is a palindrome!"; ///if a palindrome
  36. }else{
  37. cout<<string <<" is not a palindrome!"; /// if not a palindrome
  38. }
  39. cout << "\nThank you for using this program";
  40. return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement