Given a string, get a truncated string of length up to 12. The task is ambiguous, as it doesn't say anything about whether or not 12 should include terminating null character or not. None the less, let's see how one would achieve this in various languages. Let's start with python3 import sys print(sys.argv[1][:12]) Simple enough, in essence given first argument, print it up to length 12. As an added this also deals with unicode correctly that is if passed arg is 車賈滑豈更串句龜龜契金喇車賈滑豈更串句龜龜契金喇, it will correctly print 車賈滑豈更串句龜龜契金喇. (note these are just random Unicode strings to me, no idea what they stand for). In C things are slightly more verbose, but in essence, I am going to use strncpy function: #include <stdio.h> #include <string.h> void main(int argc, char *argv[]) { char res[12]; strncpy(res,argv[1],12); printf("%s\n",res); } This treats things as byte-array instead of unicode, thus for unicode test it will end up printing just 車賈滑...
a personal blog of Dimitri John Ledkov