943,552 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 631
  • C RSS
Oct 6th, 2008
0

recursion help

Expand Post »
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. }
Similar Threads
Reputation Points: 32
Solved Threads: 0
Newbie Poster
mikeregas is offline Offline
11 posts
since Oct 2008
Oct 6th, 2008
0

Re: recursion help

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. }
Reputation Points: 32
Solved Threads: 0
Newbie Poster
mikeregas is offline Offline
11 posts
since Oct 2008
Oct 6th, 2008
0

Re: recursion help

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. }
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Oct 7th, 2008
0

Re: recursion help

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Palindrome help
Next Thread in C Forum Timeline: bouncing ball project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC