Stack Exercise - String with Trash

Asked

Viewed 548 times

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..

1 answer

2


Your code has 2 problems being one more serious than the other.

  • Use strlen in a string without terminator:

    saida[strlen(saida) -1] = '\0';
    

    This is the most serious problem in the code that makes you pick up garbage in your memory. It turns out that the string has no terminator because you are precisely trying to place it, but are calculating the end of the string with strlen.

    All right, strlen only works when the string has terminator, and when it has no indefinite behavior.

    Correcting this part is easy because your i was increasing as it was putting characters in the saida and so its final value is the size of the string. So you should put the terminator also based on the i:

    saida[i] = '\0';
    
  • Do not set returns in all execution paths, which happens in the function Pop:

    char Pop(char *pilha, int *topo) {
        if (*topo == -1)
            printf("Pilha vazia!\n");  // <-- se o código vier por aqui não tem retorno
        else {   
            *topo -= 1;
            return pilha[*topo +1];
        }
    }
    

    Note that if pointed by topo for -1 the code enters the if but then has no associated return and the function expects to be returned a char, you are using in:

    saida[i++] = Pop(pilha, &topo);
    

    This error is less serious because the value is only consumed when the topo is different from -1 due to the condition that is in the while, but it’s something you should avoid doing, and that later on can result in a mistake if you don’t pay attention.

    All functions that return something must set a return value for all possible execution paths.

Note: Just as a small hint regarding nomenclature, function names usually start with small letter and so the two functions you have should be called pop and push

Browser other questions tagged

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