-3

I'm trying to find out how many times a character appear in a string.

For example:

char * line = "cat file1 | grep c | wc"

how can I find the number of times character '|' appear in the string?

I also have another question:

Is there a way to find if a string contain special character?

EX: "netbean&"

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
Thanh Nguyen
  • 11
  • 1
  • 2
  • Try the good old ``for (...)`` loop and a counter. – ThreeFx Oct 02 '14 at 06:06
  • Try the good old `strstr()`, for example. Read the man pages on those, there's a whole collection of string manipulation functions. – favoretti Oct 02 '14 at 06:07
  • Traverse string by `char` by `char` and match each char with your desired one. If match then increase counter. Final counter will show you total occurrence of char. – Jayesh Bhoi Oct 02 '14 at 06:07
  • possible duplicate of http://stackoverflow.com/questions/7349053/counting-the-number-of-times-a-character-occurs-in-a-string-in-c – Jayesh Bhoi Oct 02 '14 at 06:09

4 Answers4

2

try this :

int main(){
  const char *str = "cat file1 | grep c | wc";
  int counts[256] = { 0 };
  int i;
  size_t len = strlen(str);
  for (i = 0; i < len; i++) {
    counts[(int)(str[i])]++;
  }
  for (i = 0; i < 256; i++) {
    if(counts[i]>0)
    printf("%c occurs %d times.\n", i , counts[i]);
  }

    return 0;
}

output :

  occurs 6 times.
1 occurs 1 times.
a occurs 1 times.
c occurs 3 times.
e occurs 2 times.
f occurs 1 times.
g occurs 1 times.
i occurs 1 times.
l occurs 1 times.
p occurs 1 times.
r occurs 1 times.
t occurs 1 times.
w occurs 1 times.
| occurs 2 times.
Rustam
  • 6,485
  • 1
  • 25
  • 25
1

A slightly more terse/succinct way, just as a contrast to the (in my eyes) verbose solutions:

size_t count_tokens(const char *str, char token)
{
  size_t count = 0;
  while(*str != '\0')
  {
    count += *str++ == token;
  }
  return count;
}

This uses the fact that == produces the values 1 or 0, so we can do away witht the if in the inner loop.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

This ought to do it.

int i;
int count = 0;

int length = strlen(line);

for (i = 0; i < length; i++) {
    if (line[i] == '|') {
        count++;
    }
}
brian
  • 2,745
  • 2
  • 17
  • 33
  • Suggesting use of `strlen()` once per iteration is of course functionally correct (=it will work) but also *very* non-typical in C. – unwind Oct 02 '14 at 07:14
  • My bad, 3am. Time for bed. – brian Oct 02 '14 at 07:15
  • 1
    Of course this was an educational purpose, but it is a bad habit to use `int`s instead of `size_t`s for counting and indexing and using strlen() as loop condition, especially when it is known to not change. – user3125367 Oct 02 '14 at 07:15
-2
int c;
char *str = "hello world";
for (c = 0; *str != '\0'; str++)
    if (*str == 'o')
        c++;

if you need to compute a number of substring like "netbean&", then follows:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char *str = "hello netbean& world netbean&netbean& ooo netbean&";
    char *t;
    int c;
    for (c = 0; *str != '\0';) {
        if ((t = strstr(str, "netbean&")) != NULL) {
            c++; str = t + 1;
        } else {
            break;
        }
    }
    printf("%d\n", c);
}
Ivan Ivanovich
  • 880
  • 1
  • 6
  • 15