当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>C++进阶与实例

检验字符在字符串中的出现次数

    /*
    Check if there are same charactors in
    the string.And it may be the most
    effective way.
    */

    #include <stdio.h>

    main(int argc,char *argv[]){
       puts(argv[1]);
       char num[256]=;   /*good way to init a char array*/
       unsigned char *pos = (unsigned char *)argv[1];
       while(*pos!=0&&num[*pos]==0){
          /*
            "*pos" used for checking if the string goes end.
            "num[*pos]==0" used for checking if the char appeared before.
          */
          num[*pos++]=1;
       }
       printf("%i\n",(*pos==0?0:1));
    }


    心得:

    *用初始化数组。

相关内容
赞助商链接