recursion help

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 11
Reputation: mikeregas is an unknown quantity at this point 
Solved Threads: 0
mikeregas mikeregas is offline Offline
Newbie Poster

recursion help

 
0
  #1
Oct 6th, 2008
I need this to been done recursively, it works but I was told that it is not recursive does anyone have any ideas or suggestions

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define TRUE 1
  5. #define FALSE 0
  6.  
  7. main ()
  8. {
  9. char decision;
  10. char str[30];
  11. int isPalindrome = TRUE;
  12. int choice = TRUE;
  13. int i,j;
  14.  
  15.  
  16.  
  17. printf("Enter a word: ");
  18. gets(str);
  19. j = strlen(str) - 1;
  20. i = 0;
  21.  
  22.  
  23. while(i <= j && isPalindrome)
  24. {
  25.  
  26. if(str[i] != str[j])
  27. {
  28. isPalindrome = FALSE;
  29. }
  30. i++;
  31. j--;
  32. }
  33.  
  34. if(isPalindrome)
  35. {
  36. printf("%s is a palindrome!\n", str);
  37. }
  38. else
  39. {
  40. printf("%s is not a palindrome\n", str);
  41. }
  42.  
  43. printf("Would you like to enter another word? y or n \n");
  44. scanf("%c", &decision);
  45. if (decision == 'y') choice = TRUE;
  46. else choice = FALSE;
  47. }
  48. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: mikeregas is an unknown quantity at this point 
Solved Threads: 0
mikeregas mikeregas is offline Offline
Newbie Poster

Re: recursion help

 
0
  #2
Oct 6th, 2008
I found a recursive code in c++, but I do need it in C, if there is anyone out there that is willing to convert it that would be great. thanks

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. bool recursivePalindrome(std::string str, unsigned int index = 0) {
  6. if (index > str.length()/2) return true;
  7. if (str[index] == str[str.length()-index-1])
  8. return recursivePalindrome(str, ++index);
  9. else return false;
  10. }
  11.  
  12. int main () {
  13. cout << recursivePalindrome("yehey");
  14. cout << recursivePalindrome("w00t");
  15.  
  16. cin.get();
  17. return EXIT_SUCCESS;
  18. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: recursion help

 
0
  #3
Oct 6th, 2008
Something like this
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int recursivePalindrome(char *str, unsigned int index)
  5. {
  6. if (index > strlen(str)/2) return 1;
  7. if (str[index] == str[strlen(str)-index-1])
  8. return recursivePalindrome(str, ++index);
  9. else return 0;
  10. }
  11. int main ()
  12. {
  13. printf("%d\n",recursivePalindrome("yehey",0));
  14. printf("%d\n",recursivePalindrome("w00t",0));
  15. return EXIT_SUCCESS;
  16. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: recursion help

 
0
  #4
Oct 7th, 2008
Oh, it's much more absurd example of a recursive method than famous recursive factorial!
It's rather rough C++ to C translation. Slightly better one:
  1. int recursivePalindrome(const char *str, unsigned int index) {
  2. unsigned length = (str?strlen(str):0);
  3. ... and so with obvious code correction
... and so on with obvious code correction. The matter is that str.length() is a simple property getter. Don't add once more nonsense - strlen call in a for loop condition...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 484 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC