1-9 :
Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
point : change the entry of space putchar when there has met a space already.
#include <stdio.h> main(){ int c = 0; int inspace = 0; while((c = getchar()) != EOF){ if(c == ‘ ‘){ if(inspace == 0){ putchar(c); inspace = 1; } } else{ inspace = 0; putchar(c); } } }
count the numbers of word:
1 #include <stdio.h> 2 #define IN 1 //no ";" 3 #define OUT 0 4 5 main(){ 6 int c ; 7 int state = OUT; 8 int nc, nw, nl; 9 nc = nw = nl = 0; //注意初始化 10 11 while((c = getchar()) != EOF){ 12 nc ++; 13 if(c == ‘\t‘) 14 nl++; 15 if(c == ‘ ‘ || c == ‘\t‘ || c == ‘\n‘) { 16 state = OUT; 17 } 18 else if(state == OUT){ //if state is OUT here, means in the word, chage state and ++nw 19 state = IN; 20 nw++; 21 } 22 } 23 printf("%d %d %d",nc, nw, nl); 24 }
design a program output the result that every line contains only a word
1 #include <stdio.h> 2 #define IN 1 //no ";" 3 #define OUT 0 4 5 main(){ 6 int c ; 7 int inspace = 0; 8 while((c = getchar()) != EOF){ 9 if(c == ‘ ‘ || c == ‘\t‘ || c == ‘\t‘){ 10 if(inspace == 0){ 11 inspace = 1; 12 putchar(‘\n‘); 13 } 14 } 15 else{ 16 inspace = 0; 17 putchar(c); 18 } 19 } 20 return 0; 21 }
时间: 2024-10-11 06:41:58