Following is one way to reverse string using recursion!
#include <stdio.h>
#include <string.h>
void rev_str_recursive(char arr[], size_t iStart, size_t iLast)
{
if( iStart < iLast )
{
//swap
char temp = arr[iStart];
arr[iStart] = arr[iLast];
arr[iLast] = temp;
rev_str_recursive(arr, ++iStart, --iLast);
}
}
void main()
{
char cArray[] = {"A quick brown fox jumps over a lazy dog"};
rev_str_recursive(cArray, 0, strlen(cArray)-1);
}
Answer from AFI on Stack OverflowFollowing is one way to reverse string using recursion!
#include <stdio.h>
#include <string.h>
void rev_str_recursive(char arr[], size_t iStart, size_t iLast)
{
if( iStart < iLast )
{
//swap
char temp = arr[iStart];
arr[iStart] = arr[iLast];
arr[iLast] = temp;
rev_str_recursive(arr, ++iStart, --iLast);
}
}
void main()
{
char cArray[] = {"A quick brown fox jumps over a lazy dog"};
rev_str_recursive(cArray, 0, strlen(cArray)-1);
}
You need to modify the string, i.e. the input buffer to reverse(), instead of just printing it.
Doing this recursively seems a bit obnoxious, but should of course be possible.
Basically, I guess the printing becomes an assignment, something like this:
- Base: The reversal of an empty string is the empty string.
- Step: The reversal of a string begins by swapping the first and last characters, then recursing over the remainder of the string.
Reversing a string in C using Recursion - Stack Overflow
Reverse a sentence using recursion
[C++] Errors when trying to write a reversing string with recursion
but I keep getting errors and my code won't compile.
Tell us those errors.
I'm coding in vim and I don't know how to copy/paste.
Put set clipboard=unnamed somewhere in your vimrc. Then the system-wide clipboard (or X clipboard on Linux) will be used for yanking and pasting by default in vim. I like Shift+v for visual line selection, then y for yanking the selection.
(However, vim masters would tell you to learn motions so that you wouldn't have to use the visual mode, and they'd also tell you to read :help registers.)
C++ using Recursion for a reverse Fibonacci and binary To Decimal function
Your posts seem to contain unformatted code. Please make sure to format your code otherwise your post may be removed.
Read our guidelines for how to format your code.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
More on reddit.comVideos
Your string is actually 6 bytes long - 'h', 'e', 'l', 'l', 'o', '\0'. The last character is a null byte, which is a string terminator. It signals to functions like printf or strlen where the string ends. When you call reverse, it reverses the entire string, so now the terminator is the very first byte, and printf interprets that as an empty string.
There are two ways to fix this. Either make the index that you pass to reverse one smaller (call reverse(a, 0, n-2)), or use strlen instead of sizeof (int n = strlen(a)).
Always remember, the array index for nth element will be n-1.
Your array has 6 elements, so the index will run from 0 to 5, the last [6th] elemst being the NUL terminator.
As per your logic, the NUL terminator becomes the first element in the reversed array, hence no output as string.
Following the logic, your first call to reverse() should be reverse(a,0,n-2); to avoid the NUL being put as the first element in reversed array.
i dont understand how recursion and stack works here. pls explain. Thank you a lot.
#include <stdio.h>
void reverseSentence();
int main()
{
printf("Enter a sentence: ");
reverseSentence();
return 0;
}
void reverseSentence()
{
char c;
scanf("%c", &c);
if (c != '\n') {
reverseSentence();
printf("%c", c);
}
} Input: abcdef
Output would be like: fedcba