How to prevent me from printing garbage from an array?

0

My program read a text from a .txt and saves it in the text [TAMTEXTO] character by character, then in the separatewords () function; I separate the words with a if condition, if you find a space save the word in a two-dimensional arrangement word [NUMPALABRAS] [30] also add in the else a condition so that when you find the space fill the rest of the array with a \ 0 or end of string, this runs until the text is finished, the problem is that when sending print the two-dimensional array that saves my words prints garbage at the beginning and finally because in the .txt at the beginning of the text and at the end there is no space, What condition could you add so that it does not print that garbage?

void separarPalabras(char texto[TAMTEXTO],char palabra[NUMPALABRAS][30])
{
    int i;
    int posicionLetra = 0;
    int posicionPalabra = 0;

    for(i = 0; i<TAMTEXTO; i++)
    {
        if(texto[i] != ' '&& texto[i] )
        {
            palabra[posicionPalabra][posicionLetra] = texto[i];
            posicionLetra++;
        }
        else
        {
            palabra[posicionPalabra][posicionLetra]= '
void separarPalabras(char texto[TAMTEXTO],char palabra[NUMPALABRAS][30])
{
    int i;
    int posicionLetra = 0;
    int posicionPalabra = 0;

    for(i = 0; i<TAMTEXTO; i++)
    {
        if(texto[i] != ' '&& texto[i] )
        {
            palabra[posicionPalabra][posicionLetra] = texto[i];
            posicionLetra++;
        }
        else
        {
            palabra[posicionPalabra][posicionLetra]= '%pre%';
            posicionLetra =0;
            posicionPalabra++;
        }
    }
}
'; posicionLetra =0; posicionPalabra++; } } }
    
asked by BeetBeatBit 27.06.2017 в 05:06
source

1 answer

1

Before starting to fill your matrix with words, initialize it with %code% , for example:

palabra[10][50] = {{'
palabra[10][50] = {{'%pre%'}};
'}};
    
answered by 27.06.2017 в 07:24