unintendedjoy
unintendedjoy

Reputation: 289

Reverse array content using recursion (C Language)

Function reverseAr() takes in two arguments, an array of char (ar[]) and the size of the array (n), and then reverses the content of the array (without the use of another array). The function will then return the result to the calling function through parameter ar.

My code can't seem to run, I think I'm passing the values in between the functions wrongly, but I can't figure out where.

Here's the code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> 
#include <string.h> 
#include <math.h> 

void reverseAr(char ar[], int n);

int main()
{
    char str[128];
    int len;

    printf("\nEnter an array of characters: ");
    gets(str);
    len = strlen(str);
    printf("The reversed array of characters is = %s\n", reverseAr(str, len));

    return 0;
}

void reverseAr(char ar[], int n)
{
    char temp = ar[0];
    ar[0] = ar[n - 1];
    ar[n - 1] = temp;
    if (n > 1) 
    {
        reverseAr(++ar, n - 2);
    }
}

Expected output:

Enter an array of characters: abcde
The reversed array of characters is = edcba

Upvotes: 1

Views: 10857

Answers (4)

Vishal Kapoor
Vishal Kapoor

Reputation: 1

You can use a pointer instead of using the array name to increment (as ++ar is not valid) in the reverseAr function.

The following code should work:

/* In Main */

int main()
{
        char str[128];
        int len;

        printf("\nEnter an array of characters: ");
        gets(str);    /* use fgets instead */

        len = strlen(str) - 1;  /* length is one less than the strlen */

        printf("The reversed array of characters is = %s\n", reverseAr(str, len));

        return 0;
}

void reverseAr(char *ar, int n)
{
        /* Base case */
        if (ar > (ar + n))
        {
            return;
        }
        char temp = *ar;
        *ar = *(ar + n);
        *(ar + n) = temp;
        reverseAr(ar + 1, n - 1);
    
}

Upvotes: 0

Pandrei
Pandrei

Reputation: 4961

I think the problem is how you print the result:

void reverseAr(char ar[], int n);
printf("The reversed array of characters is = %s\n", reverseAr(str, len));

printf expects a string and you are passing it void...

You should replace that with:

reverseAr(str, len-1);
printf("The reversed array of characters is = %s\n", str);

You will notice I am passing len-1 insted of len to reverseAr that is because you want to leave the last character in the string ('\0') alone, and only switch the rest of the characters. Ultimately you want to fix the reverseAr function as well:

void reverseAr(char ar[], int i, int n)
{
    char temp = ar[i];
    ar[i] = ar[n];
    ar[n] = t;

    if (i < n) 
    {
        reverseAr(ar, i++, n - 1);
    }
}

And call it a differently: reverseAr(str,0,len-1);

Upvotes: 2

Dipika
Dipika

Reputation: 584

This is the working code for reversing string

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> 
#include <string.h> 
#include <math.h> 

int reverseAr(char ar[], int , int);

int main()
{
    char str[128];
    int len;

    printf("\nEnter an array of characters: ");

    if(fgets(str, sizeof(str), stdin) != NULL)
    {
        len = strlen(str);
        reverseAr(str, 0, len-1);
    }
    printf("The reversed array of characters is = %s\n", str );
    system("pause");
    return 0;
}

int reverseAr(char ar[], int first, int last)
{
    char temp = ar[first];
    ar[first] = ar[last];
    ar[last] = temp;

    if (first < last) 
    {
        reverseAr(ar, ++first, --last);
    }
    else
    {
        return 0;
    }
}

Upvotes: 3

unwind
unwind

Reputation: 399979

This:

char str;

gets(str);

is undefined behavior, and should give compiler warnings (or even errors) that you are ignoring.

gets() requires a pointer to an array of characters, you're giving it the value of a single character, which is not the same thing at all. It will interpret the undefined value of that character as an address, and store the input text there.

It should be:

int main()
{
  char line[128];

  printf("\nEnter an array of characters: ");
  if(fgets(line, sizeof line, stdin) != NULL)
  {
    const size_t len = strlen(line);
    printf("The reversed array of characters is = %s\n", reverseAr(line, len));
  }
  return 0;    
}

the return type of strlen() is not int, it's size_t so you should use that, and change reverseAr() accordingly.

Upvotes: 6

Related Questions