TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
C Program To Reverse a String using Stack
sachith wickramaarachchi
May 10
2016
Code
182.2
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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