练习1-8 1-9 1-10 打印制表符,换行符,空格等
1-8 编写一个统计空格、制表符和换行符个数的程序
1 #include <stdio.h> 2 3 int main() 4 { 5 int c, nc, nl, nt; //nc为换行符,nl为制表符,nt为空格 6 nc = nl = nt = 0; 7 while((c = getchar()) != EOF){ 8 if(c == '\n') 9 nc++; 10 else if(c == '\t') 11 nl++; 12 else if(c == ' ') 13 nt++; 14 } 15 printf("%d %d %d\n", nc, nl, nt); 16 return 0; 17 }
1-9 编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替
1 #include <stdio.h> 2 3 int main() 4 { 5 int c, last; 6 last = 0; 7 while((c = getchar()) != EOF){ 8 if(c != ' ') 9 putchar(c); 10 else if(last != ' ') 11 putchar(c); 12 last = c; 13 } 14 return 0; 15 }
1 #include <stdio.h> 2 3 int main() 4 { 5 int c, last; 6 last = 0; 7 while((c = getchar()) != EOF){ 8 if(c != ' ' || last != ' ') 9 putchar(c); 10 last = c; 11 } 12 return 0; 13 }
1-10 编写一个将输入复制到输出的程序,并将其中的制表符替换为\t,把回退符替换为\b,把反斜杠替换为\\
1 #include <stdio.h> 2 3 int main() 4 { 5 int c; 6 while((c = getchar()) != EOF){ 7 if(c == '\t') 8 printf("\\t"); 9 else if(c == '\b') 10 printf("\\b"); 11 else if(c == '\\') 12 printf("\\\\"); 13 else 14 putchar(c); 15 } 16 return 0; 17 }


浙公网安备 33010602011771号