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

发表于:2007-05-25来源:作者:点击数: 标签:次数字符字符串检验出现
很牛B,效率很高。 /* Check if there are same charactors in the string.And it may be the most effective way. */ #include lt;stdio.hgt; main(int argc,char *argv[]){ puts(argv[1]); char num[256]=; /*good way to init a char array*/ unsigned ch

很牛B,效率很高。

/*
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));
}

心得:

*用初始化数组。

原文转自:http://www.ltesting.net

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)