New file for Reverse String Using Stack in C

This commit is contained in:
Harshal Jain 2023-10-02 13:13:45 +00:00 committed by GitHub
parent e5dad3fa8d
commit cdc026c81b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View 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--;
}
}