Skip to content

Instantly share code, notes, and snippets.

@LCamel
Created February 9, 2012 18:44
Show Gist options
  • Save LCamel/1781939 to your computer and use it in GitHub Desktop.
Save LCamel/1781939 to your computer and use it in GitHub Desktop.
// gcc -pedantic -std=c99 a.c
#include <stdio.h>
#include <string.h>
void f(char *msg, int size, int pos) {
if (pos >= size)
return;
int len = (msg[pos] & 0x80) == 0 ? 1 : (msg[pos] & 0xe0) == 0xc0 ? 2 : 3;
char buf[len];
memcpy(buf, msg + pos, len);
f(msg, size, pos + len);
memcpy(msg + size - pos - len, buf, len);
}
int main(int argc, char* argv[])
{
char msg[] = "哈א囉世a界";
int size = strlen(msg);
f(msg, size, 0);
printf("%s\n", msg);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment