web-dev-qa-db-ja.com

Cの文字列での文字の出現回数のカウント

文字列strがあります

char *str = "100.10b.100.100";

str'.'の出現回数をカウントしたいのですが、できれば1行です。 (可能であればループなし)

私のアプローチは標準のstrchrです:

  int i = 0;
  char *pch=strchr(str,'.');
  while (pch!=NULL) {
    i++;
    pch=strchr(pch+1,'.');
  }
31
Mike

ここに私がそれをする方法があります(必要な変数の最小数):

for (i=0; s[i]; s[i]=='.' ? i++ : *s++);
73
R..

ほら、ループなし。

int c = countChars( s, '.' );

int countChars( char* s, char c )
{
    return *s == '\0'
              ? 0
              : countChars( s + 1, c ) + (*s == c);
}

しかし、実際にはループを使用します。それが使用する正しい制御構造だからです。

23
tvanfosson

OK、非ループ実装です(そして、はい、それは冗談です)。

size_t CountChars(const char *s, char c)
{
  size_t nCount=0;
  if (s[0])
  {
    nCount += ( s[0]==c);
    if (s[1])
    {
      nCount += ( s[1]==c);
      if (s[2])
      {
        nCount += ( s[2]==c);
        if (s[3])
        {
          nCount += ( s[3]==c);
          if (s[4])
          {
            nCount += ( s[4]==c);
            if (s[5])
            {
              nCount += ( s[5]==c);
              if (s[6])
              {
                nCount += ( s[6]==c);
                if (s[7])
                {
                  nCount += ( s[7]==c);
                  if (s[8])
                  {
                    nCount += ( s[8]==c);
                    if (s[9])
                    {
                      nCount += ( s[9]==c);
                      if (s[10])
                      {
                        /* too long */
                        assert(0);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  return nCount;
}
21
Michael J

これを行う標準のCライブラリ関数はなく、すべての文字を調べる必要があるため、ループなしでは困難になります。

私は明白な解決策を取ります:

int i, count;
for (i=0, count=0; str[i]; i++)
  count += (str[i] == '.');

必要に応じて、実際のコードの2行を1行に絞り込んでください。

11
Fabian Giesen

私はまだこれを関数にスローし、ソース文字列と検索する文字をパラメータ化します。

int count_characters(const char *str, char character)
{
    const char *p = str;
    int count = 0;

    do {
        if (*p == character)
            count++;
    } while (*(p++));

    return count;
}
5
cdhowie

ワンライナー(2枚)に興味がある場合:

size_t count = 0;
while(*str) if (*str++ == '.') ++count;
3
Steve Jessop

//動作するはずです。 1行でループなし。

int countChar(char *s, char letter) {
    return ((*s) ? (((*s++ == letter)? 1:0)) + countChar (s, letter)): 0);
}
2
Sanjeev

後藤はまだ好きじゃない

    int i=0,count=0;
    char *str = "100.10b.100.100";
    a:
    if(str[i]=='.')
        count++;
    i++;
    if(str[i])
    goto a;
1
Fahad Uddin

ループなしでそれを行う唯一の方法は、再帰です。楽しみのために以下が含まれていますが、ソリューションとして[〜#〜] not [〜#〜]が推奨されます。

size_t CountChars(char* s, char c)
{
    return *s ? ((c==*s) + CountChars(s+1)) : 0;
}
1
Michael J

このコードを実行するたびに、baby dijkstra cries :)

  1
  2
  3
  4 #include <ctype.h>
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 #include <string.h>
  8
  9
 10 size_t bytewise_pop_count(
 11     unsigned char * bp, size_t l
 12 ) {
 13     if ( (bp) && (l) ) {
 14         return bytewise_pop_count(bp+1, l-1) + (bp[0] ? 1 : 0);
 15     }
 16     return 0;
 17 }
 18
 19 void mercilessly_complement_bytes(
 20     unsigned char * bp, size_t l
 21 ) {
 22 /*
 23     transform
 24         0 -> 1
 25         !0 -> 0
 26 */
 27     if ( (bp) && (l) ) {
 28         bp[0] = bp[0] ? 0 : 1;
 29         mercilessly_complement_bytes(bp+1, l-1);
 30     }
 31 }
 32
 33 void xor_bytes(
 34     unsigned char * bp1, unsigned char * bp2, size_t l
 35 ) {
 36     /* stores result in bp2 */
 37     if ( (bp1) && (bp2) && (l) ) {
 38         bp2[0] ^= bp1[0];
 39         xor_bytes(bp1+1, bp2+1, l-1);
 40     }
 41 }
 42
 43
 44 int main(int argc, char * * argv) {
 45     char c;
 46     size_t count;
 47     size_t l;
 48     char * string;
 49     char * t;
 50
 51     if (argc < 3) {
 52         fprintf(stderr,
 53             "\n"
 54             "==> not enough arguments -- need char and string\n"
 55             "\n"
 56         );
 57         return EXIT_FAILURE;
 58     }
 59
 60     c = argv[1][0];
 61     string = argv[2];
 62
 63     if ( l = strlen(string) ) {
 64         t = malloc(l);
 65         memset(t, c, l);
 66         xor_bytes(string, t, l);
 67         mercilessly_complement_bytes(t, l);
 68         count = bytewise_pop_count(t, l);
 69         free(t);
 70     } else {
 71         count = 0;
 72     }
 73
 74     if ( isprint(c) ) {
 75         printf(
 76             "\n"
 77             "==> occurences of char ``%c'' in string ``%s'': %zu\n"
 78             "\n"
 79             , c, string ? string : "<NULL>", count
 80         );
 81     } else {
 82         printf(
 83             "\n"
 84             "==> occurences of char ``%hhu'' in string ``%s'': %zu\n"
 85             "\n"
 86             , c, string ? string : "<NULL>", count
 87         );
 88     }
 89     return EXIT_SUCCESS;
 90 }
1
x y z