CPL,Capter 1, 简洁的代码

1、复制字符串原型:

void copy (char *to, char *from)
{
      int i = 0;

      while (to[i++] = from[i++] != '/0')
            ;
}

 

2、字符串倒转原型

 

int reverse (char  *s)

{

       int i, j ;

       char temp;

       for (j = 0; s[i] != '/0'; ++ j)

      {

      }

 

      --j;

 

      for (i = 0; i < j; ++ i)

     {

           temp = s[j];

           s[j]  = s[i];

           s[i] = temp;

           -- j;

     }

       

     return 0;

}

 

3、用一个空格代替多个连续的空格

void onespace ()

{

           int c;

           int n_space = 0;

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

           {

                      if (c == ' ')

                      {

                                if ( n_space = = 0)

                                 n_space = 1;

                                 putchar(c);

                      }

                      if (c != ' ')

                      {

                                 n_space = 0;

                                 putchar(c);

                      }

 

           }      

}

 

 

 

阅读更多
文章标签: c
个人分类: c++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭