c程序设计语言课后习题,C程序设计语言绪论课后习题

《统计行数篇》

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值