Hello peopole , i want a program in c that can changes the order of a sentence unsing stack ,please can you help me ???

I didn't start it yet i'm a beginer and we didn't learn stack yet at university so can you help me solving this exercise with explication thank you

If you will Google "C stack" ... you may be surprised how much is at your finger tips but you never looked to see?

A 'stack' is a 'last-in-first-out' (LIFO) data structure that has many uses in programming.

So ... say you have the word "first" and you have a 'stack' to hold characters ...

and if first you put on the stack the char ...
'f'
then 'i'
then 'r'
then 's'
then 't'

Now if you pop the letters from the top of the stack and print them as you pop them, you will print:

tsrif

if you print them all, one after the other, on the same line.

There are many ways one might implement a stack.

One might be to use an array ...
and to advance a pointer into the array
after every element is 'pushed' onto the stack,
then to retreat the pointer after every pop.

Another common way could be
to use a linked list
and push_front to put new elements on the stack ...
then to pop_front ... to pop them off.

If you need help coding a linked-list in C,
you might like to start by looking at several examples linked here:
http://developers-heaven.net/forum/index.php/topic,2022.0.html

If you need to push 'words' onto a stack of 'words' ...
you could use the ClistOfString.h file (and the other files it uses freely available there) to use a Clist of dynamic C strings to hold all the words you push_front
(You could use 'erase' to pop front
or add code to pop front.)

Edited 11 Months Ago by David W

"I didn't start it yet i'm a beginer and we didn't learn stack yet at university so can you help me solving this exercise with explication thank you"

I just finished university and I know that they don't give assignments before teaching about the topic. David left you a good description(s) of how to go about this problem. The coding should be up to you, especially if this is an area that you wish to pursue. You should always strive to do your own homework, don't you think?

Hi @Rimaa21,

How are you progressing?

Did the see begiinning coding steps ... and the list examples at the link I suggested?

Can you show you show us some code that you have tried so far ... so we can see how to best help you.

Does your stack (structure) need to be an array ... or can it be a linked list?

Also, when you show us the code you have tried so far, please also show the spec's for the problem that you were assigned ... so that we can see clearly, what you were supposed to do.

A stack is an abstract concept of, as @DavidW explained, that behaves in a first in, last out manner (or last in, first out if you prefer). To add an element to a stack, you "push" it onto the stack. Subsequent "push" operations will, conceptually, push the previous elements down one, and put the new element at the "top" of the stack. Then, when you "pop" an element off the stack, the last one "pushed" onto the stack will be returned, and the rest moved up one place. In C, this can be very expensive in terms of overhead, especially if the stack gets large. Usually we either use an array, where "push" puts the new element at the end, and the "pop" returns that element. As David mentioned, you can also use a linked list, which is more expensive in terms of memory, but very easy to implement. If you are using C++, the STL (standard template library) has a stack collection type that handles all of the gnarly stuff that has to go on.

Edited 11 Months Ago by rubberman

So, using a stack to push the words of a sentence on, will reverse them as you pop them off at the end. IE, if you break up the sentence "This is a very good exercise in C programming" in to discrete words and push them onto a stack, as you pop them off and build a resulting string, what you would get would be "programming C in exercise good very a is This". Neat!

Edited 11 Months Ago by rubberman

So, to close. We don't do your homework for you. We will help you fix your more egregious mistakes, and make suggestions when we think you should be able to handle the exercise on your own. Good luck, and post your code and problems when you have some.

Hi again @Rimaa21,
Have you ever heard about the concept of 'backtracking' ?
Suppose you were moving a robot about in some maze, and recording every step by pushing them onto a stack ... but you reach a dead end ... so now ... you can backtrack ... by popping the former moves from the stack, going in the opposite direction you did on the way in ... until you find some new passage way to try ... and then begin to push all those next forward moves onto the stack ... to be able tobacktrack again ...if needed.

Note also ...
that if your reverse a series of elements twice,
you will then get back the original order.

Until we see your complete original program specifications ...
and the code you have tried so far in your attempt to implement a solution ...
we are just best guesssing what your coding problem might be.

This question has already been answered. Start a new discussion instead.