how to reversed a line of text using stack??
how to reversed a line of text using stack??
In a stack, everything in it should pop out in reverse.
First In, Last Out
http://www.cprogramming.com/tutorial...ory/stack.html
Sent from my iPadŽ
Code:#include <stdio.h> #include <stdlib.h> #include <stack> #define MAXSTACK 50 struct StackRec { int top; char entry[MAXSTACK]; }; typedef struct StackRec Stack; void initializeStack(Stack *stackPtr) { stackPtr->top = -1; } int stackEmpty(const Stack *stackPtr) { if (stackPtr->top < 0) { return 1; //return true } else { return 0; //return false } } int stackFull(const Stack *stackPtr) { if (stackPtr->top >= MAXSTACK - 1) { return 1; //return true } else { return 0; //return false } } void push(Stack *stackPtr, char item) { if (stackFull(stackPtr)) { printf("Stack is full\n"); } else { stackPtr->top++; stackPtr->entry[stackPtr->top] = item; } } float pop(Stack *stackPtr) { float item; if (stackEmpty(stackPtr)) { printf("Stack is empty\n"); } else { item = stackPtr->entry[stackPtr->top]; stackPtr->top--; } return item; } void main() { Stack theStack; float next; char str [50]; int i; initializeStack(&theStack); printf("Please enter a string: "); while (scanf("%c", str) != EOF) { push(&theStack, next); } while (!stackEmpty(&theStack)) { next = pop(&theStack); for (i=0;i<SIZE;i++){ printf("i=%d",i); printf("%c \n", str[i]); } printf("\n"); } }
I want to know how to create the main part...
You already have a main() in your program, what is it that you want help with?I want to know how to create the main part...
Thanks,
Sahil
can u help me see what's wrong i had make??Originally Posted by sahil_m
I dont have a C compiler with me right now, if you have any syntactical errors, tell me about them, or else wait for someone else to help you.
Thanks,
Sahil
don't have syntax error, but the output is not what i want......Originally Posted by sahil_m
what is SIZE??not definedCode:for (i=0;i<SIZE;i++){
scanf returns the no. of values stored.Code:char str [50]; while (scanf("%c", str) != EOF) { push(&theStack, next); }
Long time no C. I need to learn the language again.
Help a man when he is in trouble and he will remember you when he is in trouble again.
You learn in life when you lose.
Complex problems have simple, easy to understand wrong answers.
"A ship in the harbour is safe, but that's not what ships are built
for"
You are storing char,reading as float and printing as int. what are you upto.
Long time no C. I need to learn the language again.
Help a man when he is in trouble and he will remember you when he is in trouble again.
You learn in life when you lose.
Complex problems have simple, easy to understand wrong answers.
"A ship in the harbour is safe, but that's not what ships are built
for"
Few rectifications in your program.Code:#include <stdio.h> #include <stdlib.h> #include <stack> #define MAXSTACK 50 struct StackRec { int top; char entry[MAXSTACK]; }; typedef struct StackRec Stack; void initializeStack(Stack *stackPtr) { stackPtr->top = -1; } int stackEmpty(const Stack *stackPtr) { if (stackPtr->top < 0) { return 1; //return true } else { return 0; //return false } } int stackFull(const Stack *stackPtr) { if (stackPtr->top >= MAXSTACK - 1) { return 1; //return true } else { return 0; //return false } } void push(Stack *stackPtr, char item) { if (stackFull(stackPtr)) { printf("Stack is full\n"); } else { stackPtr->top++; stackPtr->entry[stackPtr->top] = item; } } char pop(Stack *stackPtr) { char item; if (stackEmpty(stackPtr)) { printf("Stack is empty\n"); } else { item = stackPtr->entry[stackPtr->top]; stackPtr->top--; } return item; } int main() { Stack theStack; char next; initializeStack(&theStack); printf("Please enter a string: "); while ((next=getchar()) != EOF && next!='\n') { push(&theStack,next); } while (!stackEmpty(&theStack)) { next = pop(&theStack); printf("%c ", next); } printf("\n"); getchar(); return 0; }
Long time no C. I need to learn the language again.
Help a man when he is in trouble and he will remember you when he is in trouble again.
You learn in life when you lose.
Complex problems have simple, easy to understand wrong answers.
"A ship in the harbour is safe, but that's not what ships are built
for"
Nice and simple.Code:stack1, stack2 while not at end of input push( stack1, this letter ) while not empty stack1 push( stack2, pop( stack1 ) )
Quzah.
Hope is the first step on the road to disappointment.