Even-though there are more methods to do the same job, below program demonstrates 2 of it.
1) Using pointers and changing the string array
2) Simple way by printing the chars in callee .
You can use #define POINTERS
to switch between the 2 options..
[terminal]
#include
#include
#include
#
define MAX_SIZE 100
/* If you want to reverse the string using 2 pointers and with array,len as args using recursion, define POINTERS, otherwise comment out the POINTERS directive.
*/
# define POINTERS
int reverse_string(char st[], int len) {
char * p, * q, tmp;
p = st;
q = st + (len – 1);
if (len & lt; = 0) {
return;
}
tmp = * p;
* p = * q;
* q = tmp;
p++;
len = len – 2;
reverse_string(p, len);
}
int reverse_string1(char st[]) {
if ( * st != ‘\0’)
reverse_string1(st + 1);
printf(” %c”, * st);
}
int main() {
char * str;
str = malloc(MAX_SIZE);
printf(“\n Enter the string to be reversed\n”);
scanf(“%s”, str);
printf(“\n You entered :: %s\n”, str);#
ifdef POINTERS
reverse_string(str, strlen(str));
printf(“\n Reversed string :: %s\n”, str);#
else
reverse_string1(str);
printf(“\n”);#
endif
}
[/terminal]