Problem in phrase search function

Asked

Viewed 96 times

3

I’m not figuring out the problem that’s in my job, she does the following:

  • I get a char vector with the '\n' in the end.
  • Then I run the vector by checking:
  • If you find in the vector the exclamation point, endpoint, or question mark, that means that I found a phrase (phrase was defined as a word ending with a dot an exclamation or a question mark), and I keep the position of the vector;
  • If I find a space, I simply hold 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 hold the position of i;
  • If I find one \n I end the loop by giving a break;

Well, that’s the job, but she’s giving a bow, the i hangs depending on the phrase, but I’m not understanding why it’s crashing, I’m not finding the error of logic.

Below is the function code:

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;
}
  • You agree that regardless of (i == 0) a parada will always receive i? Just an observation, it’s not the problem

  • But if it is going through the vector, then it always keeps the current position still, right?? because it is always guarding 1??

  • What’s the point of parada?

  • A hint: your comment on the function side is redundant. Do not need it to know that has a function statement on the :) It would be better to make a comment saying what the function ago. What do the input parameters mean? And the output value? Does the function have any side effects (like printing on the screen or changing the value of a global variable)? Notice that I said "what," not "how," which is something code C can already explain on its own. I have to confess that I still can’t understand what your job is for...

  • Have an example of a function call where your program hangs?

1 answer

2


I don’t quite understand what the point is parada, but your mistake is exactly in this variable in relation to if/else.

I did some tests here and the problem happens when you don’t fall into any if/else proposed, for example when the sentence contains comma, this way you never attribute to parada the new value of i.

My suggestion would be you change

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

To

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

Which makes the most sense to me else to increase the parada as a last resort, simply like this:

...
else {
    parada++;
}

Browser other questions tagged

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