例如:有以下字符串 字符串是:char str*="ok112009this9964541look" 提取字符串里的单词并输出。
#include<stdio.h> #include<ctype.h> #include<memory.h> int main() { char *str="ok112009this9964541look" ; char *fun=str; int state; for(;(*fun)!='\0';fun++) { while(isalpha(*fun)&&(*fun)!='\0') { putchar(*fun); fun++; state=1; } if(state) printf(" "); state=0; } return 0; }
思路:顺序读入,判断是否为字母, 是则顺序输出,不是则输出空格然后把 state置为0。 有效的解决了重复输出空格的问题