Checking number of chars in a string

This is a discussion on Checking number of chars in a string within the C++ Programming forums, part of the General Programming Boards category; How do i do something like: Code: string foo = "I dont like you"; if (number of letters "o" in ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    57

    Checking number of chars in a string

    How do i do something like:
    Code:
    string foo = "I dont like you";
    if (number of letters "o" in foo > 2) do this;
    Thanks =]

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    There are several ways. You could use the count algorithm (I think it's count). You could also do your own loop through each character checking for 'o'.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Loop through the indexes of the string comparing each character to 'o' and keeping a counter of how many 'o' characters you find. Then use that counter for your conditional. Do you know how to use a for loop? There may also be a count function I'm not fully aware of, but all that count function would be doing is likely a loop.
    Sent from my iPadŽ

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    don't forget <algorithm> I think, and count( foo.begin(), foo.end(), 'o' ); should do it

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Ok, i'll check that function, and if it doesnt fit, i'll do the loop thing. Thanks guys =]

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Tropicalia is this for homework? If so then I would advise against using the <algorithm> header unless your class has covered it.

    Code:
    	std::string testString = "10101010101";
    	int lenthOfString = testString.length();
    	for(int i=0;i<lenthOfString;i++) {
    		if(i%2) {
    			std::cout<<testString[i];
    		}
    	}
    That demonstrates how you can print a string and access the individual characters with the [] just as you access a char array. It should print only the 0s of the string (I didn't test it just wrote it here) but this is an example that should show you how to get started... any questions about it ask here.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Its not a homework... i tried this:
    Code:
    for (int a = 0; a <= MAX -1; a++){
    	if ((files[a].is_folder) && (count(files[a].name.begin(),files[a].name.end(), "/") == 3));
    {
    //DO IF ........
    }
    //DO FOR ........
    }
    And i got the compile error:
    Code:
    :\Arquivos de programas\Microsoft Visual Studio 8\VC\include\algorithm(152) : error C2446: '==' : no conversion from 'const char *' to 'int'
            There is no context in which this conversion is possible
            C:\Arquivos de programas\Microsoft Visual Studio 8\VC\include\algorithm(162) : see reference to function template instantiation 'std::iterator_traits<_Iter>::difference_type std::_Count<char*,const char[2]>(_InIt,_InIt,_Ty (&))' being compiled
            with
            [
                _Iter=char *,
                _InIt=char *,
                _Ty=const char [2]
            ]
            .\main.cpp(115) : see reference to function template instantiation '__w64 int std::count<std::_String_iterator<_Elem,_Traits,_Alloc>,const char[2]>(_InIt,_InIt,_Ty (&))' being compiled
            with
            [
                _Elem=char,
                _Traits=std::char_traits<char>,
                _Alloc=std::allocator<char>,
                _InIt=std::_String_iterator<char,std::char_traits<char>,std::allocator<char>>,
                _Ty=const char [2]
            ]
    C:\Arquivos de programas\Microsoft Visual Studio 8\VC\include\algorithm(152) : error C2040: '==' : 'int' differs in levels of indirection

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    "/" - '/' I think

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Wow! Sucefully compiled
    (the fact of use "" instead '' bring 19 lines of error is amazing )

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    People used to complain about how cryptic the error messages were when using code based on templates. The solution by the Visual C++ team was to make very long error messages describing the types used in the templates. This makes no sense for beginners, but it is actually quite helpful if you know what you are looking at.

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Not really if you think about it.

    In PHP, " is pretty much an alias of ', so in PHP it wouldn't make a difference. However, in C/C++, ' are reserved for single characters (including tabs, returns and noises), so it's only proper that it should return such an error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Replies: 8
    Last Post: 03-31-2006, 08:15 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM

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