首页 > 代码库 > The C Programming Language Exercise
The C Programming Language Exercise
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 }
The C Programming Language Exercise
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。