C:处理输入的字符
版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任。 |
1.以每行一个单词的形式打印其输入 终止控制台[Crtl+D] #include <stdio.h> #define IN 1 #define OUT 0 /*print the input with one word for each line*/ int main() { int c,state; state=OUT; while((c=getchar())!=EOF){ if((c==' '||c=='\n'||c=='\t')&&(state=IN)){ state=OUT; c='\n'; } else if(state==OUT) state=IN; putchar(c); } return 0; } #include <stdio.h> #define IN 1 #define OUT 0 #define MAXWORD 16 /*print horizontal histogram of words statistics*/ int main() { int i,c,state,length; int nword[MAXWORD]; for(i=0;i<MAXWORD;i++) nword[i]=0; state=OUT; length=0; while((c=getchar())!=EOF){ if((c==' '||c=='\n'||c=='\t')&&(state=IN)){ state=OUT; /*out of a word*/ if(length>0 && length<MAXWORD) nword[length]++; length=0; } else if(state==OUT){ state=IN; /*begin a word*/ ++length; } else ++length; /*in a word*/ } /*print the statistics*/ for(i=0;i<MAXWORD;i++){ printf("%2d:",i); while(nword[i]>0){ putchar('*'); --nword[i]; } printf("\n"); } return 0; } #include <stdio.h> #define IN 1 #define OUT 0 #define MAXWORD 16 /*print vertical histogram of words statistics*/ int main() { /*maxlength should be the most frequent word's appeared times*/ int i,j,c,state,length,maxlength; int nword[MAXWORD]; /*the statistics array*/ for(i=0;i<MAXWORD;i++) nword[i]=0; state=OUT; length=maxlength=0; while((c=getchar())!=EOF){ if((c==' '||c=='\n'||c=='\t')&&(state=IN)){ state=OUT; /*out of a word*/ if(length>0 && length<MAXWORD) nword[length]++; length=0; } else if(state==OUT){ state=IN; /*begin a word*/ ++length; } else ++length; /*in a word*/ } /*print the statistics*/ printf("\n"); for(i=1;i<MAXWORD;i++) printf("%2d ",i); printf("\n"); /*find the maxlength*/ for(i=0;i<MAXWORD;i++) if(nword[i]>maxlength) maxlength=nword[i]; for(i=0;i<maxlength;i++){ for(j=1;j<MAXWORD;j++) if(nword[j]>0){ printf(" * "); --nword[j]; }else printf(" "); printf("\n"); } return 0; } #include <stdio.h> #define IN 1 #define OUT 0 #define LENGTH 26 /*print horizontal histogram of letters statistics*/ int main() { int i,c,state; int nchar[LENGTH]; for(i=0;i<LENGTH;i++) nchar[i]=0; state=OUT; while((c=getchar())!=EOF){ if(c>=97&&c<=122){/*lower letter*/ c-=32; /*convert to upper letter */ ++nchar[c-'A']; }else if(c>=65&&c<=90) ++nchar[c-'A']; } /*print the statistics*/ printf("\n"); for(i=0;i<LENGTH;i++){ printf("%c:",i+'A'); while(nchar[i]>0){ putchar('*'); --nchar[i]; } printf("\n"); } return 0; } #include <stdio.h> #define MAXLINE 1000 /*the max length permitted for a line*/ int getline(char s[] ,int lim); /*remove lines' tail blanks*/ int main() { int len,i; char line[MAXLINE]; while((len=getline(line,MAXLINE))>0){ for(i=len-1;i>=0;i--) /*search for blanks*/ if(line[i]!=' '||line[i]!='\t') break; line[++i]='\0'; /*add new end tag*/ printf("%s",line); } return 0; } /*read a line to s[] and return it's length*/ int getline(char s[] ,int lim) { int c,i; for(i=0;i<lim-1 && (c=getchar())!=EOF && c!='\n';i++) s[i]=c; if(c=='\n') s[i++]=c; s[i]='\0'; /*the char array should include end tag '\0'*/ return i; } #include <stdio.h> # define MAXLENGTH 9 /*cut the longer line to standard length*/ int main() { int prev,c,len; len=0,prev=-1; while((c=getchar())!=EOF){ if(c=='\n'){ putchar(c); len=0; } else if(len==MAXLENGTH){/*should be cut*/ if(prev!=' '&&prev!='\t'){/*should add a connector '-'*/ printf("\n-%c",c); len=2; }else{ printf("\n%c",c); len=1; } }else{ putchar(c); len++; } prev=c; } return 0; } 本文出自 “子 孑” 博客,转载请与作者联系! 本文出自 51CTO.COM技术博客 |



zhangjunhd
博客统计信息
热门文章
最新评论
友情链接


