#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    5
    Rep Power
    0

    counting number of occuranes of specified character in string


    hi

    i know the answer to this is probably easy, and i got told to use count() as well. but my compiler keeps saying:

    Code:
    irc.cpp: In member function `void irc::parse(std::basic_string<char, 
       std::char_traits<char>, std::allocator<char> >)':
    irc.cpp:47: no matching function for call to `std::basic_string<char, 
       std::char_traits<char>, std::allocator<char> >::count(int, size_t, char)'
    if someone could tell me how to fix that or another way to count the occurances, that'd be great.

    thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "if someone could tell me how to fix that"

    The error message without the code? Now that's a new one--usually it's the other way around. Do you really expect someone to decipher your code from a cryptic error message and then fix it for you???????????????????????!

    Code:
    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    int main()
    {
    	char str[]="Hello, Harry";
    	int count = 0;
    	
    	for(int i=0; i<strlen(str); i++)
    	{
    		if(str[i]=='H') count++;
    	}
    
    	cout<<count<<endl;
    	
    	return 0;
    }
    Last edited by 7stud; August 8th, 2003 at 08:28 PM.
  4. #3
  5. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Try the following:

    Code:
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	char a[]="Hello, Harry";
    	cout << "Array: " << count(a, a+strlen(a), 'H') << endl;
    	
    	string s("Hello, Harry");
    	cout << "String: " << count(s.begin(), s.end(), 'H') << endl;
    }

IMN logo majestic logo threadwatch logo seochat tools logo