pc015.c ​
Metadata ​
- Author — Amit Dutta (amitdutta4255@gmail.com)
- Last updated — 30 Mar 2026
- License — MIT
Problem Statement ​
Problem Statement
Write a C program to reverse a string using a recursive function.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void reverseString(char [], char *, char *);
int main() {
char string[101], *p, *start, *end;
printf("Enter the string (Max: 100 char): ");
if(fgets(string, sizeof(string), stdin) == NULL) {
printf("\nError reading input.");
exit(1);
}
p = strchr(string, '\n');
if(p) *p = '\0';
printf("Before Reverse: %s", string);
start = string;
end = string + strlen(string) - 1;
reverseString(string, start, end);
printf("\nAfter reverse: %s", string);
return 0;
}
void reverseString(char str[], char *start, char *end) {
if(start >= end) {
return;
}
char temp;
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
reverseString(str, start, end);
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35