loading...

re: Daily Challenge #15 - Stop gninnipS My sdroW! VIEW POST

FULL DISCUSSION
 

And one that modifies a string inplace using C

#include <stdio.h>
#include <string.h>

char *unspin(char *input) {
    for (
        char *eoinput = input + strlen(input),
             *nextSpace=input,
             *nextWord=input ;

        nextSpace < eoinput ;

        nextWord = nextSpace + 1 ,
        nextSpace = memchr(nextWord,' ',eoinput-nextWord),
        nextSpace = nextSpace ? nextSpace : eoinput
      ) {
        if (nextSpace >= nextWord + 5) for (
            char *wordStart = nextWord, 
            *wordEnd = nextSpace-1 ;

            wordStart < wordEnd ;

            ++wordStart,
            --wordEnd
        ) {
            *wordStart += *wordEnd;
            *wordEnd = *wordStart - *wordEnd;
            *wordStart -= *wordEnd;
        }
      }
      return input;
}

int main()
{
    char s[] = "Stop gninnipS My sdroW";
    printf("%s", unspin(s));

    return 0;
}
code of conduct - report abuse