Problem in the search function

3

I am not discovering the problem that is in my function, it does the following:

  • I get a char array with '\n' at the end.
  • Then I go through the vector verifying:
  • If you find in the vector the exclamation mark, end point, or question mark, that is, I found a sentence (phrase was defined as a word that ends with a dot an exclamation or a question mark), and I save the position of the vector;
  • If I find a space, I simply keep the position;
  • If I find a digit from 0 to 9, a lowercase letter or a capital letter, if i = 0 , stop gets 0, otherwise, I simply save the position of i ;
  • If I find a \n I end the loop by giving break ;

Well, the function is this, but it's looping, i hangs depending on the phrase, but I'm not understanding why it's crashing, I'm not finding logic error.

Here is the code for the function:

int contagemFrases(char* p, int r) // Declaração da função que conta o número de frases presentes no texto.
{   
    int frases = 0, parada = 0, i;

    for(i = 0; i < r; i = parada + 1)
    {
        if(p[i] == 46 || p[i] == 33 || p[i] ==  63)
        {
            frases++;
            parada = i;
        }
        else if(p[i] == 32)
            parada = i;

        else if (p[i] >= 48 && p[i] <= 57 || p[i] >= 65 && p[i] <= 90 || p[i] >= 97 && p[i] <= 122)
        {
            if(i == 0)
                parada = 0;
            else
                parada = i;
        }
        else if(p[i] == '\n')
            break;
    }

    return frases;
}
    
asked by anonymous 11.06.2015 / 02:42

1 answer

2

I do not quite understand what parada is, but its error is exactly in that variable in relation to if/else .

I did some tests here and the problem happens when you do not fall into any of the% proposed%, for example when the sentence contains comma , so you never assign% new value of if/else .

My suggestion would be you to change

for(i = 0; i < r; i = parada + 1)

To

for(i = 0; i < r; i++)

That to me is what makes the most sense, or else create a parada to increment the i in the last case, just like this:

...
else {
    parada++;
}
    
11.06.2015 / 03:25