| | |
recursion help
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
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
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #define TRUE 1 #define FALSE 0 main () { char decision; char str[30]; int isPalindrome = TRUE; int choice = TRUE; int i,j; printf("Enter a word: "); gets(str); j = strlen(str) - 1; i = 0; while(i <= j && isPalindrome) { if(str[i] != str[j]) { isPalindrome = FALSE; } i++; j--; } if(isPalindrome) { printf("%s is a palindrome!\n", str); } else { printf("%s is not a palindrome\n", str); } printf("Would you like to enter another word? y or n \n"); scanf("%c", &decision); if (decision == 'y') choice = TRUE; else choice = FALSE; } }
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
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
C Syntax (Toggle Plain Text)
#include <iostream> using namespace std; bool recursivePalindrome(std::string str, unsigned int index = 0) { if (index > str.length()/2) return true; if (str[index] == str[str.length()-index-1]) return recursivePalindrome(str, ++index); else return false; } int main () { cout << recursivePalindrome("yehey"); cout << recursivePalindrome("w00t"); cin.get(); return EXIT_SUCCESS; }
Something like this
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> int recursivePalindrome(char *str, unsigned int index) { if (index > strlen(str)/2) return 1; if (str[index] == str[strlen(str)-index-1]) return recursivePalindrome(str, ++index); else return 0; } int main () { printf("%d\n",recursivePalindrome("yehey",0)); printf("%d\n",recursivePalindrome("w00t",0)); return EXIT_SUCCESS; }
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:
... 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...
It's rather rough C++ to C translation. Slightly better one:
c Syntax (Toggle Plain Text)
int recursivePalindrome(const char *str, unsigned int index) { unsigned length = (str?strlen(str):0); ... and so with obvious code correction
![]() |
Similar Threads
- Recursion: when do you use it? (C++)
- C++ Beginner - #include recursion problem (C++)
- Recursion (C++)
- number formatting using recursion (Java)
- Need help with recursion and arrays (C++)
- powers of two, recursion. (C)
- Create menu from properties file by recursion (Java)
- Recursion (C)
Other Threads in the C Forum
- Previous Thread: Palindrome help
- Next Thread: bouncing ball project
Views: 484 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
* adobe api append array arrays bash binarysearch char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest homework i/o ide infiniteloop initialization interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list lowest matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scheduling segmentationfault send single socketprogramming spoonfeeding stack standard strchr string student suggestions system test testautomation unix urboc user whythiscodecausesegmentationfault win32 win32api windows.h






