-5
#include<conio.h>
#include<stdio.h>
void main() 
{
    char strr[10]="hello";
    char *st;
    printf("%s",strr);
    st=strv(strr);
    printf("%s",st);
}
char* strv(char *str)
{
   static int i,j;
   char a;
   if(str+1!=NULL) 
   {
      a=*str;
      i++;
      strv(++str);
     *(str-i+j)=a;
     j++;
     return str;
   }
}

there's a error in code "conflicting types for strv" and i can;t figure out why.please respond with solution to reverse an array by recursion and without using any other array or pointer

Karthick
  • 1,010
  • 1
  • 8
  • 24
Aviral Ahuja
  • 205
  • 1
  • 2
  • 14

3 Answers3

0

Given the constraints, at least an O(n^ 2) algorithm is possible. The function locates the end, memorizes the last character, replaces it by zero for the next level and swaps array beginning and the original last character after the inner calls have finished.

void rev(char *a) {
   int l=strlen(a);
   if (l<2) return;  // nothing to swap if l==0 or l==1
   char b=a[l-1];    // have to remember the ending character
   a[l-1]=0;         // and have to set it to zero for next level
   rev(a+1);
   a[l-1]=a[0];
   a[0]=b;
}

To solve the compile error, the easiest method is to place the subroutine before main().

Aki Suihkonen
  • 19,144
  • 1
  • 36
  • 57
  • Looks like O(n) to me if you precalculate the length and just pass it around. – Eugene Sh. Sep 20 '17 at 15:15
  • Already removed the comment -- but giving the length as parameter is pretty much the same as having another pointer around; In a way, it violates the spirit of the rules... – Aki Suihkonen Sep 20 '17 at 15:23
  • Well, I see it as a very idiomatic recursion technique and an optimization in zero cost. Well, that's my opinion. – Eugene Sh. Sep 20 '17 at 15:30
0

The solution is to make a swap function and go from the begin, end of the array to the middle. While the first "iterator" or "pointer" is less than the end "pointer" continue the swaping.

My advice, when you use the code change the 0 in my code and the 4 in my code to variable, for example : begin end and it will make your code readable and Flexible for changes

This is my code:

void swap(char* first, char* last){
    if(first > last){
        return;
    }
    char tmp = *first;
    *first=*last;
    *last=tmp;
    swap(first+1 , last -1);
}

int main() 
{    char strr[10]="hello";
    char *st;
    swap(&strr[0],&strr[4]);
    printf("%s",strr);
    return 0;
}
Bizzu
  • 449
  • 3
  • 17
-2

You need to add a deceleration for you function "strv".

Because the function strv is implemented after the function main, inside the scope of the function main, "strv" in not known.

You should add a deceleration for the function strv above the function main:

char* strv(char *str);

General rule of thumb: ALWAYS add a deceleration of functions in order to avoid these cases.

Complete example:

#include<conio.h>
#include<stdio.h>

char* strv(char *str);

void main() 
{
    char strr[10]="hello";
    char *st;
    printf("%s",strr);
    st=strv(strr);
    printf("%s",st);
}

char* strv(char *str)
{
    static int i,j;
    char a;
    if(str+1!=NULL) 
    {
        a=*str;
        i++;

        strv(++str);
        *(str-i+j)=a;
        j++;
        return str;
    }
}
Idan
  • 333
  • 2
  • 8