《统计行数篇》
1-8:编写一个统计空格制表符和换行符个数的程序
#include
int main(){
int c ;/*用于接收下一个要输入的字符*/
int cb, ct, cl;
c = cb = ct = cl = 0;
while((c = getchar()) != EOF){
if(c == ' ')
cb++;
if(c == '\t')
ct++;
if(c == '\n')
cl++;
}
printf("cb=%d, ct=%d, cn=%d\n", cb, ct, cl);
}
1-9:编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替
#include
#define NONBLANK 'a'
int main(){
int c, lastc;
c = 0;
lastc = NONBLANK;
while((c == getchar()) != EOF){
if(c != ' ' || lastc != ' ')
putchar(c);
lastc = c;
}
}
1-10:编写一个将输入复制到输出的程序,并将其中制表符替换为\t,把回退符替换为\b,把反斜杠替换为\\
#include
int main(){
int c = 0;
while((c == getchar()) != EOF){
if(c == '\t')
printf("\\t");
else if(c == '\b')
printf("\\b");
else if(c == '\\')
printf("\\\\");
else
putchar(c);
}
}
《单词计数篇》
统计行数,单词数和字符数,这里对单词的定义比较宽松,它是任何其中不包含空格制表符和换行符的字符序列
#include
#define IN 1
#define OUT 0
/*统计输入中的行数,单词数和字符数*/
int main(){
int c, nl, nw, nc, state;
c = nl = nw = nc = 0;
state = OUT;
while((c = getchar()) != EOF){
++nc;
if(c == '\n')
cl++;
if(c == '\t' || c == ' ' || c =='\n')
state = OUT;
else if(state = OUT)
state = IN;
++nw;
}
printf("nl=%d, nw=%d, nc=%d",nl, nw, nc);
}
1-11(略)
1-12
编写一个程序以每行一个单词的形式打印其输入
#include
#define IN 1
#define OUT 0
int main(){
int c, state;
state = OUT;
while((c = getline()) != EOF){
if(c == ' ' || c == '\t' || c == '\n')
if(state == IN){ //finish the word
putchar('\n');
state = OUT;
}else if(state == OUT){//begin the word
state = In;
putchar(c);
}else{
putchar(c); //inside the word
}
}
}
编写程序统计各个数字空白符和其他字符出现的个数
#include
int main(){
int i, c, nwhite, nother;
int ndigit[10];
for(i = 0; i < 10; i++)
ndigit[i] = 0;
while((c = getchar()) != EOF){
if(c >= '0' || c <='9')
++ndigit[c - '0'];
else if(c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
nother;
}
for(i = 0; i < 10; i++)
printf("%d\n",ndigit[i]);
printf("nwhite = %d,nother = %d", nwhite, nother);
}
读入一组文本行,并把最长的文本行打印出来
#include
#define MAXLENGTH 1000
int getLine(char s[], int lim);
void copy (char s[], char t[]);
int main(){
int len;
int max;
char line[MAXLENGTH];
char longest[MAXLENGTH];
max = 0;
while((length = getLine(line,MAXLENGTH)) > 0){
if (len > max){
max = len ;
copy(longest,list);
}
}
if(max > 0)
printf("longest:%s",longest);
return 0;
}
/*读取一行文本行,并返回该文本行的长度*/
int getLine(char s[], int lim){
int c, i;
for(i = 0; i < lim && (c = getchar()) != EOF && c != '\n'; i++)
;
if(c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
/*将数组中的内容拷贝到临时的数组中*/
void copy (char s[], char t[]){
int i = 0;
while(t[i++] = s[i++])
;
}
1-16:删除每个输入行末尾的空格以及制表符并删除完全是空格的行
#include
#define MAXLINE 1000
int getLine(char s[], int lim);
int remove(char s[]);
int main(){
char line [MAXLINE];
while(getLine(line, MAXLINE)){
if(remove(line) > 0){
printf()
}
}
return 0;
}
int getLine(char s[], int lim){
int i, c;
for(i = 0; i < lim && (c = getchar()) != EOF && c != '\n'; i++)
;
if(c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
int remove(char s[]){
int i;
i = 0;
while(s[i++] != '\n')
;
--n; // 回退到'\n'前一格
while(i >= 0 && (s[i] == ' ' || s[i] == '\t'))
--i;
if(i >= 0){
s[++i] = '\n';
s[++i] = '\0';
}
return i;
}