How do i check if a string is in uppercase ?

Sep 12, 2012 at 2:26pm
I'm looking for a way to check if a string is in uppercase.
and when there are no lowercase characters it may not write to a file.
so what i try reach is:
if the string = Test write it to a file cause it has a lowercase character.
if the string = TEST do not write it cause it has no lowercase characters.

Please correct me if i posted this in the wrond section, or my Title is misleading.

and sorry for bad english. i'm dutch.

thanks in advance.
Last edited on Sep 12, 2012 at 2:27pm
Sep 12, 2012 at 2:34pm
Sep 12, 2012 at 2:37pm
What is string? Is it a character array or standard class std::string?

You can use standard algorithms std::any_of, std::all_of, std::none_of

For example

if ( std::any_of( std::begin( string ), std::end( string ), []( char c ) { return ( islower( c ) ); } ) ) /* write it to a file */;
Sep 12, 2012 at 2:37pm
thanks i'll give it a try
Sep 12, 2012 at 2:47pm
what is string ?
String Is std::string.
what i'm trying to do is filter out. the Uppercase words.
if my string = test then write it to file.
if my string = Test then write it to file.
if my string = TEst then write it to file.
if my string = TESt then write it to file.
but if my string = TEST then don't write it to the text file.

thanks is advance
Last edited on Sep 12, 2012 at 2:48pm
Sep 12, 2012 at 2:55pm
Thanks for all the help.
here is a solution to decide if the word is in uppercase or if the word contains a lowercase character:

#include <locale>
#include <iostream>
#include <string>
using namespace std;

int main()
{
string Test;
int count = 0;
cout << "String ?: ";
cin >> Test;
cin.get();
for(int i = 0; i < Test.length(); i++)
{
char c = Test[i];
if(isupper(c) == true)
{
count += 1;
}
}

if(count == Test.length())
{
cout << "The word is in uppercase characters" << endl;
} else
{
cout << "The word contains " << count << " lowercase characters" << endl;
}
cin.get();
}

if u know a more efficient or shorter way to do this , please tell me.

thanks in advance.
correct me if i did anything wrong
Last edited on Sep 12, 2012 at 2:59pm
Sep 12, 2012 at 3:00pm
Your solution is a bad solution. You need not to test all characters of the string that they are all uppercase. It is enought to see that at least one character is lower case. I already showed you how to do it with the standard algorithm std::any_of. It is better to use standard algorithms instead of numerous loops that shall be examined that to understand what they are doing.
Last edited on Sep 12, 2012 at 3:02pm
Sep 12, 2012 at 3:13pm
oke thanks. i'll do
Topic archived. No new replies allowed.