💻 Program Code

/* Program of reversing a string using stack */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAX 20

int top = -1;
char stack[MAX];
char pop();
void push(char);

main()
{
	char str[20];
	unsigned int i;
	printf("Enter the string : " );
	gets(str);
	/*Push characters of the string str on the stack */
	for(i=0;i<strlen(str);i++)
		push(str[i]);
	/*Pop characters from the stack and store in string str */
	for(i=0;i<strlen(str);i++)
		str[i]=pop();
	printf("Reversed string is : ");
	puts(str);
}/*End of main()*/

void push(char item)
{
	if(top == (MAX-1))
	{
		printf("Stack Overflow\n");
		return;
	}
	stack[++top] =item;
}/*End of push()*/

char pop()
{
	if(top == -1)
	{
		printf("Stack Underflow\n");
		exit(1);
	}
	return stack[top--];
}/*End of pop()*/

                        

🖥 Program Output

Enter the string : HelloWorld
Reversed string is : dlroWolleH
Press any key to continue . . .
                            

📘 Explanation

NILL
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.