1 /*for(i = 0; i < lim - 1 && ((c = getchar()) != ' ' && c != EOF; ++i) 2 s[i] = c*/ 3 4 5 while(i < lim - 1) 6 { 7 while((c = getchar()) != EOF) 8 { 9 while((c = getchar() != ' ') 10 ++i; 11 s[i] = c; 12 } 13 }
用while来做!
书上答案
1 enum loop {NO, YES}; 2 enum loop okloop = YES; 3 i = 0; 4 while(okloop == YES) 5 if(i >= lim - 1) /* outside of valid range ?*/ 6 okloop = NO; 7 else if ((c = getchar()) == ' ') 8 okloop = NO; 9 else if(c == EOF)/*end of file ?*/ 10 okloop = NO; 11 else{s[i] = c 12 ++i 13 }
目测差不多!
还是我的答案不行?