Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

converting a string to lowercase/uppercase depending upon the count of upper/lower case characters in it

i have to output a string in all uppercase if count of uppercase characters in it is more otherwise lowercase string will be shown if lowercase characters are more in the string,in case both characters are equal i will print the string in lowercase only
this the code i have written , but , it’s not giving desired output,like it is returning the same string back to me , please help me with the errors

here’s the code,i am using c++.

string s;
cin>>s;
int uc=0,lc=0;
for (int i = 0; i < s.size(); i++)
{       
    if(isupper(s[i])){
        uc++;
    }
    else{
        lc++;
    }
}
if(uc>lc){
    for (int j = 0; j < s.size(); j++)
    {
        toupper(s[j]);

    }    
    cout<<s<<endl;
}
else{
    for (int k = 0; k < s.size(); k++)
    {
        tolower(s[k]);
    }
    cout<<s<<endl;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

After your first loop, do this:

string result = (uc>lc) ? toupper(s) : tolower(s);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading