4 ms·
My take on in-place reversing words in a string. Would appreciate any suggestions for improvement :) #include <string.h> void reverseRange(char *i
by masterj 15y ago
My take on in-place reversing words in a string. Would appreciate any suggestions for improvement :)
#include <string.h>
void reverseRange(char *i, char *j) {
char temp;
for (--j; i <= j; ++i, --j) {
temp = *i;
*i = *j;
*j = temp;
}
}
void reverseString(char *str) {
char *ptr;
// reverse the whole string
reverseRange(str, strchr(str,'\0'));
// reverse each individual word
for (ptr = str; *ptr; str = ptr + 1) {
for (ptr = str; *ptr != ' ' && *ptr; ++ptr) {}
reverseRange(str, ptr);
}
}