Battery Exercise - String with garbage

2
  

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

    
asked by anonymous 18.10.2018 / 03:38

1 answer

2

Your code has 2 problems being one more serious than another.

  • Use strlen in a string without terminator:

    saida[strlen(saida) -1] = '
    saida[i] = '
    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];
        }
    }
    
    ';
    ';

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

    Well, strlen only works when the string has a terminator, and when it does not have undefined behavior .

    Correcting this part is easy since its i has increased as you have entered characters in saida and therefore its final value is the size of the string. You should then also place the terminator based on i :

    saida[i++] = Pop(pilha, &topo);
    
  • Do not set returns on all execution paths, which happens in the Pop function:

    saida[strlen(saida) -1] = '
    saida[i] = '
    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 the given by topo is -1 the code enters if but then has no associated return and the function expects a char to be returned, which is used in:

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

    This error is less serious because the value is only consumed when topo is different from -1 because of the condition that is in while , but it is something that should avoid doing, and that later may result an error if you do not pay attention.

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

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

    
18.10.2018 / 12:33