# Thread: Checking number of chars in a string

1. ## 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. 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. 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.

4. don't forget <algorithm> I think, and count( foo.begin(), foo.end(), 'o' ); should do it

5. Ok, i'll check that function, and if it doesnt fit, i'll do the loop thing. Thanks guys =]

6. 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. 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. "/" - '/' I think

9. Wow! Sucefully compiled
(the fact of use "" instead '' bring 19 lines of error is amazing )

10. 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. 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.