scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

0027-strtok.c (661B)


      1 #include <assert.h>
      2 #include <stdio.h>
      3 #include <string.h>
      4 
      5 /*
      6 output:
      7 testing
      8 test1
      9 one
     10 two
     11 three
     12 four
     13 test2
     14 one
     15 three
     16 test3
     17 one
     18 done
     19 end:
     20 */
     21 
     22 void
     23 test(char *msg, char *fmt)
     24 {
     25 	char *s, buff[50];
     26 
     27 	puts(msg);
     28 
     29 	strcpy(buff, fmt);
     30 	for (s = strtok(buff, "-+"); s; s = strtok(NULL, "+-")) {
     31 		switch (atoi(s)) {
     32 		case 1:
     33 			puts("one");
     34 			break;
     35 		case 2:
     36 			puts("two");
     37 			break;
     38 		case 3:
     39 			puts("three");
     40 			break;
     41 		case 4:
     42 			puts("four");
     43 			break;
     44 		default:
     45 			puts("error");
     46 			break;
     47 		}
     48 	}
     49 }
     50 
     51 int
     52 main()
     53 {
     54 	puts("testing");
     55 	test("test1", "-+001--0002++3+-4");
     56 	test("test2", "001--+-+-+-3+-");
     57 	test("test3", "001");
     58 	puts("done");
     59 	return 0;
     60 }