2
Write an algorithm, using a Stack, that inverts the letters of each word of a text finished by a dot (.) preserving the order of words. By example, given the text: THIS EXERCISE IS VERY EASY. The answer should be: ETSE OICICREXE AND OTIUM LICAF
#include <stdio.h>
#include <locale.h>
#include <conio.h>
#include <string.h>
char Pop(char *pilha, int *topo);
void Push(char valor, char *pilha, int *topo);
int main() {
setlocale(LC_ALL, "portuguese");
char pilha[100], aux, saida[100];
int topo = -1, i = 0;
do {
aux = getche();
if (aux != ' ' && aux != '.') {
Push(aux, pilha, &topo);
} else {
while (topo > -1) {
saida[i++] = Pop(pilha, &topo);
}
saida[i++] = ' ';
}
if (aux == ' ') aux = '-';
} while (aux != '.');
saida[strlen(saida) -1] = '\0';
printf("\n%s\n", saida);
return 0;
}
char Pop(char *pilha, int *topo) {
if (*topo == -1)
printf("Pilha vazia!\n");
else {
*topo -= 1;
return pilha[*topo +1];
}
}
void Push(char valor, char *pilha, int *topo) {
if (*topo == 100)
printf("Pilha cheia!\n");
else {
*topo += 1;
pilha[*topo] = valor;
}
}
This code is working. However, depending on the phrase, it prints garbage together. Can anyone tell me where the problem is? Thank you! I’ve tried reading normal and moving to battery using a for.. but it didn’t work..