C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
C Program To Reverse a String using Stack
WhatsApp
sachith wickramaarachchi
May 10
2016
185.2
k
0
0
In a data structure stack allow you to access last data element that you inserted to stack,if you remove the last element of the stack,you will be able to access to next to last element..
We can use this method or operation to revers a string value.
*create an empty stack
*one by one push all characters of string to stack
*one by one pop all characters from stack and put them back to string.
#include <stdio.h>
#include <string.h>
#define max 100
int
top,stack[max];
void
push(
char
x){
// Push(Inserting Element in stack) operation
if
(top == max-1){
printf(
"stack overflow"
);
}
else
{
stack[++top]=x;
}
}
void
pop(){
// Pop (Removing element from stack)
printf(
"%c"
,stack[top--]);
}
main()
{
char
str[]=
"sri lanka"
;
int
len = strlen(str);
int
i;
for
(i=0;i<len;i++)
push(str[i]);
for
(i=0;i<len;i++)
pop();
}
reverse string
c program
data structure
Up Next
C Program To Reverse a String using Stack