C程序设计语言习题(1-12)

统计行数、单词数,字符数的程序:

 #include<stdio.h>

 #define IN    1   /*在单词内*/
#define OUT 0 /*在单词外*/ int main()
{
int c, nl, nw, nc, state; state = OUT;
nl = nw = nc = ; //nl:行数 nw:单词数 nc:字符数
while((c = getchar()) != EOF) {
++nc;
if(c == '\n')
++nl;
if(c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
return ;
}

【练习1-12】 
编写一个程序,以每行一个单词的形式打印其输入

 #include<stdio.h>

 #define IN    1   /*在单词内*/
#define OUT 0 /*在单词外*/ int main()
{
int c, state;
state = OUT; while((c =getchar()) != EOF) {
if(c == ' ' || c == '\n' || c == '\t') {
if(state == IN) {
putchar('\n');
state = OUT;
}
} else if(state == OUT) {
state = IN;
putchar(c);
} else {
putchar(c);
}
}
return ;
}
上一篇:a标签# 和javascript:;的区别


下一篇:git 错误解决