A Program to Reverse String Without Recursion
In this example, we are giving a program to reverse a string without the recursion.
Program to to reverse a string without the recursion:
#include <stdio.h>
#include <string.h>
void reverse(char*, int, int);
int main() {
char str[100];
//input string
gets(str);
reverse(str, 0, strlen(str) - 1);
printf("%s\n",str);
return 0;
}
void reverse(char*a, int start, int end) {
char x;
// recursive condition
if(start >= end)
return;
// simple swapping
x = *(a+start);
*(a+start) = *(a+end);
*(a+end) = x;
// recursive function
reverse(a, ++start, --end);
}
Output:
Input a string: abc Reverse of the string: cba