| | |
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: 485 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
#include * ansi array arrays asterisks binarysearch calculate changingto char cm command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkwinlinux hacking hardware histogram homework inches include incrementoperators input iso kernel keyboard km lazy linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number opendocumentformat opensource overwrite owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string structures student systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






