# Thread: Counting number of lowercase.[NEWBIE]

1. ## Counting number of lowercase.[NEWBIE]

hi, i am new in this forum. i have a problem with my code. can someone correct it for me and give some advice.

i want to find out the number of lowercase letters used in the name...

this is the code tat i stuck with....

For instance:

Code:
```int main()
{
string name;

cout<<"Enter name: ";
cin>>name;

for(int i=0; name[i]; i++)
{
if(islower(name[i]))
{
int sum = 0;

sum++;
cout<<sum;
}
}```
and the output is 11 if i enter [Ben] as my input...
i want to count the it so the ouput will display 2.
can someone help me on this please.

2. Why you're initalizing your array to 0 and outputing inside of the loop?

[EDIT] Sorry it took me so long to come to this conculsion. I'm dead tired right now.

3. why are you displaying sum inside your if statement? which is inside your for statement? at this point you do not yet want to display sum. declare sum outside of your conditionals (up at the top by where you declare name wouldn't be a bad spot ) and then move cout<<sum; to just after the closing of your for statement. (btw, that may not be your complete code, but you need another closing bracket at the end and a return 0; wouldn't be a bad idea)

4. Basically, if you don't quite understand your output, I can tell you that it was correct. Just think about how you have your code.

You input "Ben"

Everytime it finds a lowercase letter, it sets sum to zero. Adds 1 to it and outputs that number. It did that one 'e' and it did that on 'n'. 1 and 1 is what? 11.

5. >i want to find out the number of lowercase letters used in the name...
The easy way:
Code:
```#include <iostream>
#include <algorithm>
#include <cctype>

int main()
{
std::string name = "Julienne";

std::cout<< std::count_if ( name.begin(), name.end(), islower ) <<'\n';
}```
The safe easy way:
Code:
```#include <iostream>
#include <algorithm>
#include <cctype>

struct is_lower {
int operator() ( int c )
{
return islower ( c );
}
};

int main()
{
std::string name = "Julienne";

std::cout<< std::count_if ( name.begin(), name.end(), is_lower() ) <<'\n';
}```
Why does the safe version put a seemingly unnecessary wrapper around islower? Because cctype declares a one argument version of islower, but iostream also declares a two argument version for use with locales. By passing just the name islower, which is overloaded in two headers that are both included, this creates an ambiguity that could cause problems for you. So a unique wrapper is used to call the single argument version and ensure that everything plays nicely. That said, the easy way works just peachy on a lot of compilers.

6. >> for(int i=0; name[i]; i++)

The string class is not null terminated and can have embedded nulls. Don't use name[i] as the control of the loop. Use i < name.length() or i < name.size(). Otherwise you might end up with a crash by passing an invalid index to the string's operator[].

7. Originally Posted by Daved
>> for(int i=0; name[i]; i++)

The string class is not null terminated and can have embedded nulls. Don't use name[i] as the control of the loop. Use i < name.length() or i < name.size(). Otherwise you might end up with a crash by passing an invalid index to the string's operator[].
I had that in my original post but wasn't quite sure if it was as invalid as I thought it was. Thanks for clearing that up.

8. Originally Posted by Prelude
The safe easy way:
Code:
```#include <iostream>
#include <algorithm>
#include <cctype>

struct is_lower {
int operator() ( int c )
{
return islower ( c );
}
};

int main()
{
std::string name = "Julienne";

std::cout<< std::count_if ( name.begin(), name.end(), is_lower() ) <<'\n';
}```
Could you also do something like:
Code:
```#include <iostream>
#include <algorithm>
#include <cctype>
#include <string>

bool is_lower(string&);

int main()
{
std::string name = "Julienne";

std::cout<< std::count_if ( name.begin(), name.end(), is_lower() ) <<'\n';
}

bool is_lower(string& c)
{
return islower(c);
}```
to eliminate the problem with the overloaded islower function? If so, how do they differ, and what situations would each be more suited to?

9. >Could you also do something like:
Why would you have to do something like that? By passing a string to count_if, each item passed to the predicate is a single character, so a reference to a string is a type mismatch. You're also suggesting basically the same thing that I did except using a function instead of a function object.

But yes, I did forget to include <string> in my examples.

10. Originally Posted by Prelude
By passing a string to count_if, each item passed to the predicate is a single character, so a reference to a string is a type mismatch.
oops. So if I were to do it this way, I should use a char instead, or...?

Originally Posted by Prelude
You're also suggesting basically the same thing that I did except using a function instead of a function object.
That's what I was wondering. I don't know much about structs or classes yet, and so far setting up a new predicate as I did is the only way I've learned to do it.

Thanks.

11. I'm supprised no one has mentioned anything like this:

Code:
```int main()
{
string name;

cout<<"Enter name: ";
cin>>name;

for(int i=0, length=name.size(); i<length; i++)

if(name[i] >= 'a' && name[i] <= 'z')

sum++;

cout<<sum;

return 0;
}```

12. Originally Posted by The Brain
I'm supprised no one has mentioned anything like this:
I'm not. You could do that, but when there is a predefined function for you, what's the point?

13. Originally Posted by SlyMaelstrom
I'm not. You could do that, but when there is a predefined function for you, what's the point?
because its usually smaller to do things yourself rather than including a header file that may contain quite a bit of code that you dont really need. its like including string.h when the only thing your using it for is to see how many chars are in a string

14. On the Dev-C++ compiler.

Code:
```#include <cctype>
int main() {}
// 17,535 bytes```
Code:
```int main() {}
// 17,535 bytes```
Even if it did make a small difference. This isn't 1965, people aren't worried about a handful of bytes on their harddrive.

15. than including a header file that may contain quite a bit of code that you dont really need.
AFAIK, the preprocessor will only pull the code you use from the header, so that's not an issue.