C:1-5-3

1-5-3 行计数

实验一:

#include<stdio.h>   //复制到gcc中易少 “#incl”

main()

{

  int c;

  int nb=0,nt=0,nl=0;

  while((c=getchar())!=EOF)

  {

    if( c== ' ' )  //空格的表示单引号之间必须是敲击一次空格键

    nb++;

    if(c=='/t')   //也可写成  else if (c=='/t') 

    nt++;      //等同“++nt”

    if(c=='/n')  //: else if(c=='/n')

    nl++;

  }

   printf("nb=%d,nt=%d,nl=%d/n",nb,nt,nl) ;

}

 

调试  Cfree环境 : ctrl+Z输入(^z)EOF

      Gcc环境ctrl+D 输入(^z)EOF

      空格:按 space 

      制表符:按 tab 

      换行:  enter

输出都正常。统计空格、制表符和换行符的个数的程序。

 

实验二:

#include<stdio.h>

#define NONBLANK 'a'   //符号常量NONBLANK负责把整型变量

main()                 //lastc 初始化为一个任意的非空格字符

{

  int c,lastc;

  lastc=NONBLANK;   //初始化变量lastc

  while((c=getchar())!=EOF)// C记录当前输入字符的ASCALL码值

   {

   if(c!=' ')

   putchar(c);

   if (c==' ')

        if (lastc!=' ')

        putchar(c);

   lastc=c;      //刷新lastc,使其记录前一个输入字符的ASCALL码值

   }

}

 

 

 

#include<stdio.h> 

#define NONBLANK 'a'

main()

{

  int c,lastc;

 lastc=NONBLANK;

  while((c=getchar())!=EOF)

   {

     if(c!=' ')

   putchar(c);

     else if (lastc!=' ')   //注意“if-else”格式与“if-if-if”对比

          putchar(c);

     lastc=c;

   }

}

 

 

#include<stdio.h>

#define NONBLANK 'a'

main()

{

  int c,lastc;

  lastc=NONBLANK;

  while((c=getchar())!=EOF)

   {

     if(c!=' '||lastc!=' ')  //用逻辑或(OR)操作符|| 实现

   putchar(c);

     lastc=c;  //不能加else,否则无法将多个空格用一个空格代替

   }

}

 

调试:将输入复制到输出的程序且将多个空格用一个空格代替

/*Replace  string  of  blanks  with a  single  blank*/

 

实验三:

#include<stdio.h>

/*replace tabs and backspaces with visible characters*/

main()

{

  int c;

  while ((c=getchar())!=EOF) //不要忘了 "()”调整优先级 

  {

 

    if(c=='/t')   //  字符常量 用单引号’‘引起

       printf("//t");

    else if(c=='/b')     //??? 

            printf("//b");

         else if(c=='//')    //???

                   printf("");

              else putchar(c);

  };

}

 

#include<stdio.h>

/*replace tabs and backspaces with visible characters*/

main()

{

  int c;

  while ((c=getchar())!=EOF) //不要忘了 "()”调整优先级 

  {

 

    if(c=='/t')   //  字符常量 用单引号’‘引起

        printf("//t");

    if(c=='/b')    //??? 

        printf("//b");

    if(c=='//')   //???

        printf("");

       if(c!='/t'&&c!='/b'&&c!='//')    // if(c!='/b')

                                     //  if(c!='/t')

             putchar(c);               //   if(c!='//')

  };

}

调试:将输入复制到输出并将制表符和回退符以可见的方式显示出来。

 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值