mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
New file for Reverse String Using Stack in C
This commit is contained in:
parent
e5dad3fa8d
commit
cdc026c81b
23
data_structures/stack/Reverse_String_Using_Stack.c
Normal file
23
data_structures/stack/Reverse_String_Using_Stack.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int stack[50]; //initialize stack
|
||||||
|
char str[100]; //initialize the string
|
||||||
|
int top = -1; //give top=-1 so that when first element is scanned index will be 0
|
||||||
|
int i = 0;
|
||||||
|
printf("Enter string :- ");
|
||||||
|
fgets(str,100,stdin); //input of string from the user
|
||||||
|
while(str[i]!='\0')
|
||||||
|
{
|
||||||
|
top++;
|
||||||
|
stack[top] = str[i]; //store each index char of str in stack
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
i = top; //give i=top so that we can print the string in reverse
|
||||||
|
printf("\nReverse of the string is :- \n");
|
||||||
|
while (i != -1)
|
||||||
|
{
|
||||||
|
printf("%c",stack[i]); //printf reverse string
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user