| |
You are here: Experts > Computing/Technology > C/C++ > C > Reverses string contents using stack in C
C - Reverses string contents using stack in C
Expert: Prince M. Premnath - 10/26/2009
Question May i know how to write C programming using stack for reverses string using stack (the top and bottom elements exchange positions, the second last element exchange positions, and so forth until the entire stack is reversed). And how to use 2nd stack?
Answer Hi dear mazlee!
Need not to go for that much heavy processing ,say reversing , swapping kinda things
just have a look at this simple program , uses stack to reverse the sentence
#include<stdio.h>
#include<conio.h>
#define MAX_STK 200
void main()
{
int i = -1;
int Counter = 0;
char Stack[MAX_STK];
char* srcString = "this is a text string";
char* temp = srcString;
while(*srcString)
Stack[++i] = *srcString++;
Counter = i;
while(Counter >= 0)
printf("%c",Stack[Counter--]);
getch();
}
Note: Get back to me in case of issues
Thanks and Regards
Prince M. Premnath
Add to this Answer Ask a Question
|
|