1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | // Author: Sheng Yu // Time: 02/03/2013 // // C solution for problem 1.2 in Cracking the Coding Interview 5th eidtion. // // Test environment: gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-52) // #include <stdio.h> #include <string.h> #define MAXSIZE 256 void reverse(char *str){ char *str_begin = str; char *str_end = str+strlen(str)-1; // Point to the last char char temp; while(str_begin < str_end){ temp = *str_begin; *(str_begin++) = *str_end; *(str_end--) = temp; } return; } int main(int argc, char * argv[]){ if(argc != 2){ printf("Unsuitable arguments!n"); return 1; } char str[MAXSIZE]; str[MAXSIZE-1] = ' '; strncpy(str,argv[1],MAXSIZE-1); printf("ready to work: %sn",str); reverse(str); printf("Work result: %sn",str); return 0; } |
Unofficial C Solution to Problem 1.2 in Cracking the Coding Interview (5th Edition)
3 Feb