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 stringgets(str);reverse(str, 0, strlen(str) - 1);printf("%s\n",str);return 0;}void reverse(char*a, int start, int end) {char x;// recursive conditionif(start >= end)return;// simple swappingx = *(a+start);*(a+start) = *(a+end);*(a+end) = x;// recursive functionreverse(a, ++start, --end);}
Output:
Input a string: abc Reverse of the string: cba