0

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

3
Contributors
2
Replies
20
Views
2 Years
Discussion Span
Last Post by Sarkurd
0

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

1

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;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.