scc

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

0027-strtok.c (681B)


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