Click here to Skip to main content
15,846,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I did the question using recursion method
# include <stdio.h>
void reverse(char *str)
{
	
   if (*str)
   {
       reverse(str+1);
       printf("%c", *str);
   }
}
int main()
{
   char a[] = "Hello Klu";
   reverse(a);
   return 0;
}


What I have tried:

So i have tried the question in recursion method so my question is if they give an function such as
void reverse_string( char *string );
to use should we only use recursion method or is there any other methods?
Posted
Updated 5-Nov-22 9:00am

An easy approach is to loop to middle characters while changing the indexed char and the end minus the indexed char. Take case when string uneven count of chars.
 
Share this answer
 
Reversing a string is not a recursive algorithm: it's iterative - and should be dealt with via a simple loop. And your function doesn't technically reverse the string - it prints the string backwards in a really inefficient way.

Thing about what you have to do: convert "ABCDE" to "EDCBA"
Which means swapping the first and last characters, then swapping the second and fourth characters - the middle one doesn't need any swapping because the string length is odd so there is a single character in the middle.
So ... write a loop that runs from 0 to the (string length / 2 - 1) in an index called i
Each time round the loop, swap the character at i with the character at string length - i + 1

After the loop, it's all swapped.

If you have to print the string in reverse, that's even easier and even lass recursive: just loop through it starting from the last character and working forward in a loop.

Recursion is a powerful tool, but like all tools it shouldn't be used everywhere! You wouldn't use a microscope to hang a picture on a wall, would you? No - you'd use a hammer because it's the appropriate tool.
 
Share this answer
 
Comments
merano99 5-Nov-22 8:21am    
+5, really inefficient way - exactly that
The function
C
void reverse_string( char *string );

does not allow a recursive solution because void was given as return value.

The best would be to reorder the string in place by exchanging the characters from front to back.

If the function also does the output of single characters you could also write a recursive solution. But then each iteration outputs only one character at a time, not the whole string. This would be very inefficient, since many unnecessary recursions would be necessary and the output of single characters is also very slow.
 
Share this answer
 
v3
Quote:
is there any other methods
You could copy the characters one by one, to a new array from last to first. But given that the question is not very clear, any answer is probably acceptable.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900