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
char strcan only hold a single character.