Turning into bool function

I believe writing this code where it returns a bool in the function would be more preferred. Wouldn't it be more efficient as well?
Correct me if I am wrong.
Also I do not really get the steps behind converting this into a bool statement.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 int checkLowercase(char *strPtr)
{
	int occurs = 0;
	while (*strPtr != '\0')
	{
		if (isupper(*strPtr))
		{
			occurs++;

		}
		
		strPtr++;
	}
	return occurs;
}
Why would you want to convert this to a bool function? You're returning a count. You can't do that with a bool function.

Now, if you don't care about the count, but only whether the string contains at least one lower case character, then a bool function makes sense:
1
2
3
4
5
6
7
8
int bool hasLowercase (char *strPtr)  //  Note the change in prefix
{   while (*strPtr != '\0')
    {  if (islower(*strPtr))  // Note the change to islower
          return true;  // lower case char found, no point in checking further
	strPtr++;
    }
    return false;  // No lower case char found
}


The bool function I posted is slightly more efficient, but only because it does an early return at line 4.

edit: Corrected the return type.
Last edited on
Should have been more clear. I thought the function name would have specified what I was doing.

I don't care about the count and thanks for the response.

now returning a bool means changing the function from an int to bool correct?
Last edited on
Figured it out. Thanks AbstractionAnon!
Sorry. My return type was incorrect. Should have been a bool. I've edited the post.
Topic archived. No new replies allowed.