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
intbool hasLowercase (char *strPtr) // Note the change in prefix
{ while (*strPtr != '\0')
{ if (islower(*strPtr)) // Note the change to islower
returntrue; // lower case char found, no point in checking further
strPtr++;
}
returnfalse; // 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.