Reversing a string in c with recursion

后端 未结 11 751
遥遥无期
遥遥无期 2021-01-22 09:47

I have written code to reverse a string in c... it works fine but I can\'t return the reversed string in the main() function.

#include

        
相关标签:
11条回答
  • 2021-01-22 10:08
    #include <stdio.h>
    #define MAX 100
    int main()
    {
    char str[MAX], *rev;
    scanf("%s", str);
    rev = reverse(str); 
    printf("The reversed string is : %s\n", rev);
    return 0;
    }
    char *reverse(char ch[])
       {
        static char r[MAX];
        static int i=0;
        if(*ch == '\0') return "";
        else 
       {
        reverse(ch+1);
        r[i++]=*ch;
       }
        return r;
       }
    
    0 讨论(0)
  • 2021-01-22 10:09

    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:

    1. Base: The reversal of an empty string is the empty string.
    2. Step: The reversal of a string begins by swapping the first and last characters, then recursing over the remainder of the string.
    0 讨论(0)
  • 2021-01-22 10:16
    #include <iostream>
    using namespace std;
    
    reverse( char *str)
    {
        if (*str!='\0')
        {
           reverse(str+1);
           cout<<*str;
        }
    //cout<<*str   when i am just printing here then why this is printing after one space ??
    }
    
    int main()
    {   
        string a ;  
        cin>>a;   
        reverse(&a[0]); 
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-22 10:17

    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);
    }
    
    0 讨论(0)
  • 2021-01-22 10:21

    Here is another way to reverse a string using recursion:

    void reverseString(char* dest, char *src, int len) {
        if (src == NULL || len == 0)
            return;
    
        reverseString(dest, src + 1, len - 1);
        strncat_s(dest, len + 1, src, 1);
    }
    

    You can call like that:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define STRING "Let's try this one."
    #define SIZE 20
    
    void main() {
    
        char* src = (char*)malloc(SIZE);
        char* dest = (char*)malloc(SIZE);
    
        strcpy_s(dest, SIZE, "");
        strcpy_s(src, SIZE, STRING);
    
        reverseString(dest, src, strlen(src));
        /* Do anything with dest. */
        // printf("%s\n", dest);
    
        free(src);
        free(dest);
    }
    
    0 讨论(0)
  • 2021-01-22 10:24

    use sprintf it will print your reversed string into buffer.

    #include<stdio.h>
    
    char *b;
    
    main()
    {
      char a[17]="abcdefg";
      char buffer[17];
      buffer[0]= '\0';
      b = buffer;
      reverse(a);
      printf("%s\n",buffer);
    }
    int reverse(char *a)
    {
       if(*a!='\0')
       {   
         reverse(a+1);
         sprintf(b,"%c",*a);
         b++;
       }
    
    }
    
    0 讨论(0)
提交回复
热议问题