How about using
strcspn
and
strlen
:
...
char str[] = "..."; const char sep[] = " "; int len = 0;
char *word = str;
while(NULL != (word = next_word(sep, word, &len))) swap_wordchars(word, len)
printf("%s", str);
int next_word(char *sep, char *word, int *len)
{
word += *len;
while(0 == (*len = strcspn(word, sep)) && *word) word++;
return *len ? word : NULL;
}
void swap_wordchars(char* str, int len)
{
int sentry = len / 2;
for(int i = 0; i < sentry; ++i) swap_chars(str+i, str+len-1-i);
}
void swap_chars(char *a, char *b)
{
char c = *a;
*a = *b;
*b = c;
}
I did now half of your homework - you have to do some boiler plate coding (add appropriate includes and the main function), compile, link, run.
And you might need to explain the code to someone ;-)
Cheers
Andi