开发者

Counting number of occurrences of a char in a string in C

I have the string str

char *str = "100.10b.100.100";

I want to count the occurrences of '.' in str, preferably a one-liner. (If possible no loops)

My approach would be the standard strchr:

  int i = 0;
  char *pch=strchr(str,'.');
  while (pch!=NULL) {
    i++;
    pch=strchr(pch+1,'.');
  开发者_Go百科}


Here's the way I'd do it (minimal number of variables needed):

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


OK, a non-loop implementation (and yes, it is meant as a joke).

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;
}


Look, ma, no loops.

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

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

But, I'd actually use a loop, since that's the correct control structure to use.


Without loops is going to be hard since there's no standard C library function that does this and you need to look at all chars :)

I'll take the obvious solution:

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

Feel free to squeeze the two lines of actual code into one if you have to :)


If you're keen on a one-liner (well, two-):

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


I'd still throw this in a function, parametrizing the source string and the character to search for.

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

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

    return count;
}


//I guess it should work. One line and no loop.

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


The only way to do it without a loop is recursion. The following is included for fun, but is NOT recommended as a solution:

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


I dont like goto,still,

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


everytime you run this code 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 }


If you really want a one-liner:

  • keeping str intact (in case you want to use it later)

    for(i = 0, c = 0; str[i] != '\0'; (str[i] == '.')? c++: 0, i++);

  • if you don't care about str variable

    for(c = 0; *str != '\0'; (*str == '.')? c++: 0, str++);

    • additionally you can subtract previous strlen to make it usable again

but as you can see, it's pretty ugly so I would advise doing something like this:

for(i = 0; str[i] != '\0'; i++)
{
    if(str[i] == '.')
        ++c;
}

or

for(c = 0; *str != '\0'; str++)
{
    if(*str == '.')
        ++c;
}


Using memchr:

size_t nOccurrences(const char* buffer, const char character, size_t bufferLength) {
    size_t result = 0;
    size_t residualLength = bufferLength;
    
    do {
        void* start = (void*)(buffer + bufferLength - residualLength);
        void* occurrence = memchr(start, character, residualLength);
        if(NULL == occurrence) {
            break;
        }
        ++result;
        residualLength = bufferLength - ((char*)occurrence - buffer) - 1;
    } while(1);
    
    return result;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜