You are here:

C/Reverses string contents using stack in C

Advertisement


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

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise


I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience

Research over 6+ Years

Organizations
CG-VAK Softwares and Exports Limited

Education/Credentials
Masters in Computer Applications

©2012 About.com, a part of The New York Times Company. All rights reserved.