How do i check if a string is in uppercase ?

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
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 */;
thanks i'll give it a try
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
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;
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;

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
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
oke thanks. i'll do
Topic archived. No new replies allowed.