Whenever I'm scanning a string in C I should use "strlen ()", right?

7

It is common to see in C executions that people need to parse and / or manipulate the contents of a string , so we need to be sure that it will not exceed its limit. It is very common to make a for ranging from 0 to strlen() - 1 , after all this is the function that takes the number of characters of the string . In every language it is like this (unless it has a foreach ).

Is there any problem in doing so? Is it better?

    
asked by anonymous 24.11.2016 / 12:53

1 answer

10

The linked question has the definition of strlen() . This function counts how many characters the string has. If that's all you want then it can be used, it's meant for that. But only use when that number is what you want. Whenever it is desired to scan the string potentially until its end it should not be used, you should not get the string string to limit how far is not required.

In fact, it is bad for application performance. In an exercise okay to use, but in real applications should not be used. You have to be creative and use another better-performing way.

C does not store string size, would complicate it a little or would waste memory doing this. Remember that decades ago memory was very scarce. The only workaround was to put a character at the end of the string ( strlen() ) to indicate that it has ended. This ensures that any string can be represented and only one byte of overhead is needed to indicate its end.

What's the problem? While languages that store the size next to the text can say the string size with complexity O (1) and you already have the information, in C we have complexity O (N), that is, you have to read the whole string >, go counting to get your size.

A naive implementation of the function would be:

size_t strlen (char *str) {
    size_t len = 0;
    while (*str != '
for (int i = 0; i < strlen(string); i++) {
    if (string[i] == ' ') {
        count++;
    }
}
') { str++; len++; } return len; }

actual implementation becomes more complicated .

Using a for as a limit on a strlen() is probably an error because it will read the entire string to find the size every time you need that number and potentially read through the string to perform what you want. It's duplicate work.

Someone might be thinking that just taking for from within strlen() , so just read once. But it is not a solution.

It's possible for some compilers to do an optimization and solve this on their own. But I even doubt it for a basic reason. C has strings , so size can change inside the loop, getting the size before can be a huge mistake if the loop manipulates the string size. Even if that is the solution we will still scan the string twice, once to find the size and the other to do what you want.

The solution is to do what is there in the strlen() function. Walk through the string starting at 0 and scroll down to find stlen() . Do not predetermine when it should stop, let the simple condition determine the stop. What you are doing is precisely bringing into your loop what is done within %code% .

This even solves the problem of the size of the string change within the loop (it will probably change to smaller, almost always for larger memory corruption).

So if you want to know how many spaces there are inside the string , you can make it look more "obvious":

size_t size = strlen(string);
for (int i = 0; i < size; i++) {
    if (string[i] == ' ') {
        count++;
    }
}

You can improve:

while (*string != '
size_t strlen (char *str) {
    size_t len = 0;
    while (*str != '
for (int i = 0; i < strlen(string); i++) {
    if (string[i] == ' ') {
        count++;
    }
}
') { str++; len++; } return len; }
') if (*string == ' ') { count++; } string++; }

Or you can do the right thing:

size_t size = strlen(string);
for (int i = 0; i < size; i++) {
    if (string[i] == ' ') {
        count++;
    }
}

Not using pointers in C is a serious error. C does not work well with abstractions, it's a crude language. Abstractions often contain costs, often difficult to assess, not part of the philosophy of language. Where abstractions exist, they must be done in a way that is intuitive to use and does not cause excessive cost. Delete abstraction, delete intermediates.

Running% c is the most misused function of programming.

    
24.11.2016 / 12:53