How to create a for loop to check every character in a string for lowercase letters?

1.2k views Asked by At

Hi I was wondering if someone could please help me understand why every time I try to run this code it runs but will abort as soon as it gets to it. It only does it when I have i inside the PassWord.at(i). When I replace it with an int such as 0 or 1 it works correctly but only checks that character. I need to be able to check the entire string to see if it has a lowercase character. Thanks!

int check = 0;

for(int i = 0; i <= PassWord.size(); i++)
{
    if(islower(PassWord.at(i)) != 0)
    {
        check++;
    }
}
1

There are 1 answers

0
Mark Schlosser On BEST ANSWER

Your loop control should be

for(int i = 0; i < PassWord.size(); i++)

The way you had it, you would index outside the array. You can only index from 0 to size-1.