Write an algorithm, using a Stack, that inverts the letters of each word of a finished text by period (.) preserving the order of words. Per example, given the text: THIS EXERCISE IS VERY EASY. The output 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] = '#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] = '%pre%';
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;
}
}
';
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 someone tell me where the problem is? thanks! I already tried to read normal and go to stack using a for .. but it did not work ..