-
Recursion
Code:
char func(int , int ,char *, char *);
int main (void)
{
char str[MAX];
char *temp;
int y=0, counter=0;
cout << "Enter String: ";
fgets(str, MAX, stdin);
temp = new char [strlen(str)];
*temp = UselessFunction(counter,y, temp, str);
strcpy(str,temp);
strrev(temp);
if(strcmp(str,temp)==0)
cout << "\n\n Palindrome\n";
else
cout << "\nnot a Palindrome\n";
return 0;
}
char func(int counter, int y, char* temp, char* str)
{
if (!isspace(str[counter])) //removing spaces
{
temp[y] = str[counter];
y++;
}
if (counter < strlen(str)-1)
UselessFunction(++counter,y, temp, str);
return *temp;
}
returning garbage :confused:
-
ok i think this line is giving me problem:
Code:
temp = new char [strlen(str)];
gives me wrong length
but i dont see why?
-
umm nm, i was placing the null at wrong place :rolleyes:
-
I'm going to guess:
Code:
temp = new char [strlen(str)];
->
Code:
temp = new char [strlen(str)+1];
And don't forget <cstring> and <cctype>.
-
> *temp = UselessFunction(counter,y, temp, str);
Is there a reason why you overwrite the first char of the result string, when you return from the function?