-
-
Notifications
You must be signed in to change notification settings - Fork 94
Open
Description
strlen(example) function is called in each iteration of the loop to determine the length of the string example. However, calculating the length of the string repeatedly in each iteration can be inefficient, especially if the length remains constant throughout the loop.
for (int iter_char = 0; iter_char < strlen(example); iter_char++) {
int len = strlen(example);
for (int iter_char = 0; iter_char < len; iter_char++) {
example[iter_char] = tolower(example[iter_char]);
if (example[iter_char] == ' ') {
if (example[iter_char + 1] != ' ') {
split[word][character] = '\0';
character = 0;
word++;
}
continue;
} else {
split[word][character++] = example[iter_char];
}
}
By calculating the length of the string outside the loop and storing it in the variable len, you avoid the overhead of recomputing the length in each iteration. This can lead to a significant improvement in performance, especially for long strings.
mdahamshi
Metadata
Metadata
Assignees
Labels
No labels