What logic do I use to write this program in C?

Asked

Viewed 76 times

0

I’m solving a list of exercises on stacks and queues and stuck on a question where ask to write the program according to pseudo code of the question.

I have very little experience with C, so I’m not getting around to it. I wanted your help from the O.R..

Program:

Programa inverte var P : Pilha;

n : inteiro;

s : literal;

Início

Imprima('Entre com uma frase: ');

Leia(s)

Inicia(P); //{Torna a pilha vazia}

Para n de 1 até COMP(s) faça

PUSH(P, s[n]);

Enquanto não Vazia(P) faça //{pilha vazia, para}

Imprima (POP(P));

Fim-enquanto;

Fim
  • 1

    And what is your question? How far have you come? Give us your current code so we can help you solve your problem, and not do your exercise.

1 answer

4

Good afternoon Leonardo,

First let’s break the problem into small pieces:

  • What your program should do?

By the title put, "reverses", it is easy to deduce that he wants to reverse a string using a stack structure, where the user will type a word/phrase and the same should be printed upside down at the end of the execution.

  • But the final, how can I do it using batteries?

Cells are data structures of the type LIFO [Last in, First out], that is, the last entry must be the first to exit.

Illustration:

inserir a descrição da imagem aqui

From this we can deduce that to reverse your string using a stack, just stack and pop.

  • Tips for the implementation:

Since you don’t have much experience with C, I strongly suggest that you study the application of malloc, do...while() and, mainly, pointers.

Translation of commands from your pseudo code:

Print = printf(); Read = scanf(); While = while(); To = for(;;); [Loop repetition as well as while]

I hope I’ve helped, hugs and good studies!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.