i write a code that coun t the number of upper case letter in a string there is a problem
my code count space as capital letter
code is below

#include<iostream>
#include<conio.h>
#include<cstring>
#include<string.h>
using namespace std;
class Letters{

    private:
        char str[20];
        int num;
    public:
        Letters(){

            str[0]='\0';
            num=0;
        }

        void inputString(){

            cout<<"Enter any String = ";
            cin.getline(str,20);
        }
        void capitalLetters(){

            int i=0;
            char ch;
            while(str[i]!='\0'){

                if(str[i]==toupper(str[i]))
                num++;
                i++;
            }
        }

        void show(){

            cout<<"\nString Is = "<<str<<endl;
            cout<<"Number Of Capital Letters = "<<num<<endl;
        }

};
int main()
{
    Letters ob;
    ob.inputString();
    ob.capitalLetters();
    ob.show();
    getch();
    return 0;
}

and wrong output of my program is below

Enter any String = hello world

String Is = hello world
Number Of Capital Letters = 1

how can i solve it

Recommended Answers

All 2 Replies

Charcodes of capital letters is 65-90. Replace line 29 to if(int(str[i])>=65 && int(str[i])<=90)

For checking a letter if capital we use std::isupper.
std::toupper used to convert lower letters to captital.

std::string str("Hello");
int count = 0;
for(int i = 0; i != str.size(); ++i)
{
    if(std::isupper(str[i]))
        ++count;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.