loop to identify how many lower case letters exist in an input

I am trying to construct a loop that will return the amount of lower case letters within a input, for example if the input were abc123, the input would return 3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string.h>

#include <iostream>

using namespace std;

int main(){

// create a string

string s("The man is fit");

cout << s << endl;
}


I have not got very far and hit a snag, I have tried to output this string and it fails on the build but I dont know why
Did you even read the Error?
You don't include string.h, but string for C++ strings.
closed account (ypfz3TCk)
You should use the header <cctype> and the function 'isupper'!

There is an example on this website - see: http://www.cplusplus.com/reference/clibrary/cctype/isupper/

Correction - the function you need is 'islower' - you should be able to easily find many examples of the code to achieve what you want.
Last edited on
@Neil:
He can use !(isupper())
I would use a loop to go through each character in the string, then use an if statement to check if the letter is lowercase (by checking if its ASCII value is between 97-122, this is the range for lowercase letters)

1
2
if((int)string[i]>97 && (int)string[i]<122)
count++;


Then later on just cout "count". I'm unsure about the syntax but I believe the logic is correct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
main()
{
char c[65];int i=0,j=0;
printf("/n/nplease enter your string");
scanf("%[ -z]",c); /*to select the characters entered as valid or invalid [a-b]...
a is the starting character and b is the ending character in the sequence of ASCII table
for eg if u write [A-B] it means ASCII A-65 to ASCII Z-90 [ -z] wud mean
ASCII space to ASCII small z */
while(c[i]!='\0')
{if ((islower(c[i]))>0)  //to check whe
{j++;}
i++;
}
printf("The number of lower case characters that were found in the string entered were %d",j);
getch();
}
Last edited on
closed account (ypfz3TCk)
hi, im sure mj1709's code is correct, but i don't consider the syntax to be C++ - it seems to be C!
Topic archived. No new replies allowed.