occurences in a string

This is a discussion on occurences in a string within the C Programming forums, part of the General Programming Boards category; is there a c function that counts the number occurences of a character in a string. For example, given somthing ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    25

    occurences in a string

    is there a c function that counts the number occurences of a character in a string. For example, given
    somthing like...

    (apple, p)

    will return 2 (for two p's)?

    Thanks,

    Bill

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    no but I'll make one for you hang on...

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    writing your own should not be a problem.
    Code:
    int check(char *str_ptr, char ch)
    {
       int count = 0;
    
       while(*str_ptr)
       {
          if(*str_ptr == ch)
             count++;
          str_ptr++;
       }
       return count;
    }
    You would call this like
    Code:
    total = check("apple", 'p');

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int countThem(char inputString[BUFSIZ], char inputLetter)
    {
     int j = -1;
     int k = 0;
    
     do
     {
      j++;
      if(inputString[j] == inputLetter)
      {
        k++;
      }
     }
     while(inputString[j]); 
     return k;
    }
    
    int main(void)
    {
     char a[20] = "apple";
     int i = countThem(a,'p');
     printf("%d",i);
     return 0;
    }
    Last edited by Brian; 01-29-2002 at 02:05 PM.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    mine's better :P

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Brian - yours doesn't work if the requested letter's the first in the string...

    Also, they're both case sensitive.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    oops lol . *quick edit*

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    Also, they're both case sensitive.
    That wasn't specified in the question, maybe he wants it case sensitive.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 10:20 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21