Join 132,121 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,975 people online right now. Registration is fast and FREE... Join Now!
I have written this code and it is supposed to tell us if the string entered is a palindrome. it must be done using recursion and it needs to use a string reverse function and it hasto be written in .c I dont know where i am going wrong with this.
CODE
#include <stdio.h> #include <string.h>
void string_reverse(char* str, int first, int last); void palindrome(char* str, char newstring, int first, int last);
int main (void) { char str[1024]; char newstring[1024]; char answer [5];
printf("Palindrome Checker\n"); // Read a string from the user
do { printf("Enter a string\n"); scanf("%s", str);
//check if it is palidrome by if the reverse equal the entered one string_reverse(str, 0, strlen(str)-1); printf("Result: %s\n", str); scanf("%s", newstring);
void palindrome(char* str, char newstring, int first, int last);
void palindrome(char* str, char newstring, int first, int last) { //base case if(strlen <=1) printf("The string you entered was a palindrome\n"); //base case if(first == last && str[first]+1 != str[last]-1); printf("The string you entered was a palindrome\n"); return;
//recursive if(first != last && str[first]+1 != str[last]-1) printf("The string you entered was not a palindrome\n"); }
int isPalindrome(char* str, int end) { if (end<1) { return 1; } // we've passed center, it's true if (str[0] != str[end]) { return 0; } // character doesn't match, it fails // note, we move the front one, the end one // then the end another one, to allow for the front movement return isPalindrome(str+1, end-2); }
It's still C++, rather than the poster's C. Worse, it's still calling the string length function every single time; a waste of effort. The division by 2 is also unnecessary.
But the length()/2 is used to exit half way along the string, as opposed to going through the whole thing~ it saves going through the entire string, which improves the speed of the end result, right?
And I edited my post, I realised it was C++ when the OP was looking for C.
I see what you mean about calling the length() method each time though. I never thought of that when I was writing it~
for(unsigned long i=0; i<10; i++) { test(10000 + (i*1000)); }
return EXIT_SUCCESS; }
The results:
CODE
Testing for: 10000 1 0 millisecs for isPalindrome. 1 300000 millisecs for recursivePalindrome.
Testing for: 11000 1 0 millisecs for isPalindrome. 1 330000 millisecs for recursivePalindrome.
Testing for: 12000 1 0 millisecs for isPalindrome. 1 380000 millisecs for recursivePalindrome.
Testing for: 13000 1 0 millisecs for isPalindrome. 1 430000 millisecs for recursivePalindrome.
Testing for: 14000 1 0 millisecs for isPalindrome. 1 510000 millisecs for recursivePalindrome.
Testing for: 15000 1 0 millisecs for isPalindrome. 1 570000 millisecs for recursivePalindrome.
Testing for: 16000 1 0 millisecs for isPalindrome. 1 630000 millisecs for recursivePalindrome.
Testing for: 17000 1 0 millisecs for isPalindrome. 1 720000 millisecs for recursivePalindrome.
Testing for: 18000 1 0 millisecs for isPalindrome. 1 820000 millisecs for recursivePalindrome.
Testing for: 19000 1 0 millisecs for isPalindrome. 1 880000 millisecs for recursivePalindrome.
It should be noted that the original test was more optimistic, but the string didn't want to deal:
CODE
Testing for: 10000 1 0 millisecs for isPalindrome. 1 290000 millisecs for recursivePalindrome.
Testing for: 100000 1 0 millisecs for isPalindrome. terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_alloc /home/bam/.netbeans/6.1/bin/dorun.sh: line 103: 12413 Aborted "$pgm" "$@"
Curious, I took the std::string one out and just tested mine. I got this far before I blew the heap:
CODE
Testing for: 1000000 1 20000 millisecs for isPalindrome.