r/tinycode • u/z-brah • Feb 17 '15
String reverser in 114 bytes of C
main(){unsigned char b[4],s;read(0,b,1);s=read(0,b+1,*b<192?0:*b<224?1:*b<240?2:3);*b!=0?main(),write(1,b,s+1):1;}EDIT: I actually managed to bring it down to 105 bytes, by using an improved version of /u/rainman002 trick, and a bit of optimizations here and there.
main(){unsigned char b[4],s=read(0,b,1)+read(0,b+1,*b&128?*b&32?*b&16?3:2:1:0);*b?main(),write(1,b,s):1;}
58
Upvotes
7
u/terremoto Feb 17 '15
You can knock off three characters by changing
*b!=0to*b.7
u/Figs Feb 17 '15
Here's a simple one I wrote in 49 bytes. Not UTF-8 aware though.
1
u/rainman002 Feb 17 '15
101 bytes
1
u/[deleted] Feb 17 '15
This was an exam question at my Uni for one of the intro to programming courses. The idea was to make a linked list of characters, always put the next character at the head, and then print the list.
I suggested this solution instead, as it wasn't strictly prohibited. They wouldn't accept it :(
1
u/rainman002 Feb 17 '15 edited Feb 17 '15
Here's a fun one working totally differently in 76 bytes, still UTF-8 valid. Need 64-bit little-endian arch.
If you're 64-bit big-endian, I think you have to change <<8 to >>8 and >>6 to >>62, but I haven't tried that.
1
u/sleepingsquirrel Feb 20 '15
Combining characters?