-
Notifications
You must be signed in to change notification settings - Fork 0
/
34_reverse_string.c
65 lines (61 loc) · 1.39 KB
/
34_reverse_string.c
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Author: Girish Gaude
* Date: 3/Jan/2020
* Desciption: Program to reverse given string
* Input: Enter the string and choese recursive method OR non-recursive method.
* Output: Display Reverse string
*/
#include<stdio.h>
#include<stdio_ext.h>
void rec(char *str, int len); //Declare function
int main()
{
char ch;
do
{
int opt; //Declare all variable
int i=0,len=0;
char str[100];
__fpurge(stdin);
printf("Enter String : \n"); //Read string
scanf("%100[^\n]",str);
printf("Select options: \n 1> Recursive\n 2> Non-Recursive\n");
scanf("%d",&opt); //Read option
while(str[i] != '\0') //Count lenght of string
{
len += 1;
i++; //Incr count
}
switch(opt)
{
case 1: //Case 1 recursive
{
printf("Recurcive Reverse String : \n"); //Read string
rec( str, len );
break;
}
case 2:
{
printf("Non-Recursive Reverse String : \n"); //Read string
for(int i=len; i>=0; i--) //Case 2 Non- recursive
{
printf("%c", str[i]);
}break;
}
default:
{
printf("Invalid Input\n"); //Print Error
}
}
printf("\nDo you want to continue.\n");
getchar();
scanf("%c", &ch);
}while( ch == 'y' ); //Run again
return 0;
}
void rec( char *str, int len)
{
printf("%c",str[len]);
if (len != 0)
rec(str,--len);
}