Мои два цента:
/* Reverses n characters of a string and adds a '\0' at the end */
void strnrev (char *txt, size_t len) {
size_t idx;
for (idx = len >> 1; idx > 0; idx--) {
txt[len] = txt[idx - 1];
txt[idx - 1] = txt[len - idx];
txt[len - idx] = txt[len];
}
txt[len] = '\0';
}
/* Reverses a null-terminated string */
void strrev (char *txt) {
size_t len = 0;
while (txt[len++]);
strnrev(txt, --len);
}
Тест № 1 - strrev()
:
char string[] = "Hello world!";
strrev(string);
printf("%s\n", string); // Displays "!dlrow olleH"
Тест № 2 - strnrev()
:
char string[] = "Hello world!";
strnrev(string, 5);
printf("%s\n", string); // Displays "olleH"