Recently, I stumbled upon the need of creating a string library for my specific use case in C. Specifically, I wanted to lowercase all characters in a string. I wondered if it is more efficient to use a for loop that increments 1 until the end of the string or is it better (a millimeter of more performance?) to increment a character pointer?
Without incrementing the char pointer:
void string_tolowercase_s(string_t *dest) {
for (int i = 0; i < dest->text_length; i++)
dest->text[i] = tolower(dest->text[i]);
}
Incrementing the char pointer:
void string_tolowercase_s(string_t *dest) {
for (char *letter = dest->text; *letter; letter++)
*letter = tolower(*letter);
}
EDIT: The string is guaranteed to contain the null terminator ('\0').