Reversing a string in c with recursion

后端 未结 11 752
遥遥无期
遥遥无期 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:26

    a little change in Emre Can Kucukoglu's answer . . . we can eliminate strncat_s

    void revstr_rec(char *sstr, char *dstr, int len)
    {
        int i = 0;
        if((! *sstr) || (! len) )
            return;
    
        revstr_rec(sstr + 1, dstr, len - 1);
        dstr[len - 1] = *sstr;
    
        return;
    }
    
    int main()
    {
        char *sstr = NULL;
        char *dstr = NULL;
    
        sstr = malloc(16);
        if(! sstr)  {
            printf("no memory . . .\n");
            return 0;
        }
        strcpy(sstr, "hello world !");
        printf("sstr: %s\n", sstr);
    
        dstr = malloc(16);
        if(! dstr)  {
            printf("no memory . . .\n");
            return 0;
        }
    
        revstr_rec(sstr, dstr, strlen(sstr));
        printf("dstr(recursive): %s\n", dstr);
    
        free(sstr);
        free(dstr);
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-22 10:31

    This code is not executable :( You define int reverse but reverse function doesnt return any value

    instead use this (using void):

    #include<stdio.h>
    
    main()
    {
      char a[17]="abcdefg";
      reverse(a);
      printf("\n");
      system("PAUSE");
    }
    void reverse(char *a)
    {
       if(*a!='\0')
       {   
         reverse(a+1);
       }
       printf("%c",*a);
    }        
    
    0 讨论(0)
  • 2021-01-22 10:31
    #include<stdio.h>
    void reverse(char *a);
    main()
    {
      char a[17]="abcdefg";
      reverse(a);
      printf("\n");
      system("PAUSE");
    }
    void reverse(char *a)
    {
       if(*a!='\0')
       {   
         reverse(a+1);
       }
       printf("%c",*a);
    }  
    
    0 讨论(0)
  • 2021-01-22 10:32
    void palindromo(char *s)
    {
        if(s[0] != '\0'){
            palindromo(s+1);
            printf("%c", s[0]);
        }
    }
    

    This is a small recursive function who print the string inverted.

    0 讨论(0)
  • 2021-01-22 10:32
    #include<stdio.h>
    #include<string.h>
    void rev(char *);
    
    int main()
    {
        char s[]="Hello";
        printf("\n%s",s);
        rev(s);
        printf("\n%s",s);
        return 0;
    }
    
    void rev(char *s)
    {
        static int i=0;
        static int j=0;
        if(j==0)                   //since static variable can be intitialized 
        {                          //only with a constant literal,to store 
            j=strlen(s)-1;         //length-1 in 1st function call, we can use 
        }                          //this trick.(condition satisfied only in 1st 
                                   //function call)
        if(i<j)
        {
            char temp;
            temp=s[i];
            s[i]=s[j];             //Analogous to for(i=0,j=l-1;i<j;i++,j--)
            s[j]=temp;             //                { //swap code }
            i++;
            j--;
            rev(s);           
        }
    }
    
    0 讨论(0)
提交回复
热议问题