-4

What is the fastest method to find the number of occurrence of character in a sub-string of string?

I have tried the normal scanning each character but the test data is too large.

daft300punk
  • 169
  • 3
  • 16
  • 1
    `strchr` in a loop while it doesn't return `NULL` – David Ranieri Mar 06 '15 at 19:54
  • Give us your code. Is the problem related to the fact that there's too much data to load into memory at once? This is very vague right now. – BaseZen Mar 06 '15 at 19:54
  • If you want to count how many times a particular character appears, then you have no choice but to look at every byte. Otherwise you might be interested by [Boyer-Moore](https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm) and in general [what grep does](https://lists.freebsd.org/pipermail/freebsd-current/2010-August/019310.html) – tux3 Mar 06 '15 at 19:56
  • Does this have to be C or can you use C++? If so then this answer is properly the most efficient way. https://stackoverflow.com/questions/3867890/count-character-occurrences-in-a-string – wybourn Mar 06 '15 at 19:56
  • @wybourn why do you ask? I think there would be a c++ tag otherwise. – Iharob Al Asimi Mar 06 '15 at 19:57

1 Answers1

1

It's very simple

unsigned int countChar(const char *string, char characterOfInterest)
 {
    unsigned int count; 

    count = 0;
    while ((string = strchr(string, characterOfInterest)) != NULL)
     {
        count  += 1;
        string += 1;
     }
    return count;
 }
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 2
    This code is calling `strchr` for each character of the string, there is no need to do that. – David Ranieri Mar 06 '15 at 20:04
  • 1
    Maybe something like `while ((p = strchr(string, characterOfInterest)) != NULL) { count++; string = p + 1; }` Note: `characterOfInterest == 0` is a special case. – chux - Reinstate Monica Mar 06 '15 at 20:09
  • or maybe `char *p=string; while ((p = strchr(p, characterOfInterest)) != NULL) { count++; p++; }` (to preserve `string` unchanged) – David C. Rankin Mar 06 '15 at 20:28
  • 1
    @DavidC.Rankin good suggestion although there is no benefit in that, IMO. – Iharob Al Asimi Mar 06 '15 at 20:31
  • @DavidC.Rankin is there any real difference with this answer, except that **iharob** wrote to `const char *string` as the argument as opposed to its target string? – Weather Vane Mar 06 '15 at 20:45
  • @iharob don't be so antagonistic. I was supporting your comment, altering the pointer provided is ok, **DavidC.Rankin** proposed the same thing with another pointer var. Why can't you just forget that I downvoted you once, but have upvoted since many times? – Weather Vane Mar 06 '15 at 20:51
  • @WeatherVane I didn't understand your comment at first, and then ... I deleted mine. And I didn't know that you downvoted one of my answers. – Iharob Al Asimi Mar 06 '15 at 20:54
  • @iharob you once complained about my downvote when I copped to it. If I was out of order, I apologise. – Weather Vane Mar 06 '15 at 21:06
  • @WeatherVane - first it is a **comment**, not an **answer**. The reason for it (generally, not with `strchr`) is to preserve the pointer to the original string. So `string` remains `string`. All commenting and answering know, that **it does not matter for this function**, but the poster may not know that failing to do so with other string functions, like `strtok`, or where `string` is dynamically allocated will cause headaches. That's why it was worth a comment. – David C. Rankin Mar 07 '15 at 03:58