简体   繁体   中英

C++ Check if whole string is uppercase

I am trying to check if the whole word is upper case, if this is true it should return true, else return false.

My current code is:

#include "UpperCaseFilter.h"
#include "ReadFilteredWords.h"
#include "ReadWords.h"
#include <locale>

bool UpperCaseFilter::filter(string word) {
    if(!word.empty()) {
        for(int i = 0; i < word.length(); i++) {
            if(isupper(word[i])) {
                return true;

            }
            else {
                return false;
            }
        }
    }
}

The problem with this code is, if i have for example HeLLO , it will return true because my last character is true. How would I only return true if the whole string is true. I did it using a counter method but it is not the most efficient.

I also tried using the all_of method but I think I dont have the correct compiler version because it says all_of isn't defined (Even with correct imports).

I'm not sure what other approaches there are to this.

Alternatively utilize the std::all_of function in combination with std::isupper in predicate:

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::string s = "HELLo";
    if (std::all_of(s.begin(), s.end(), [](unsigned char c){ return std::isupper(c); })) {
        std::cout << "Is uppercase." << '\n';
    } else {
        std::cout << "Is not uppercase." << '\n';
    }
}

Used as part of a function:

bool isUpper(const std::string& s) {
    return std::all_of(s.begin(), s.end(), [](unsigned char c){ return std::isupper(c); });
}
bool is_all_upper(const std::string& word)
{
    for(auto& c: word) 
        if(!std::isupper(static_cast<unsigned char>(c))) 
            return false;
    return true;
}

I assume that, if the string is empty, it can be considered all-uppercase.

You shouldn't have two return conditions inside your loop. Rather, you can use the loop to find problems and, if there are no problems, you'll escape the loop and can tell the user that everything was alright at the end.

In the comments you say "i believe it doesn't need to return anything if the string is empty"; however, a function with a return type, such as this one always returns something. If you don't specify a return value it will give you one, whether you like it or not. Therefore, you must decide what the output should be for every conceivable input. Accordingly, I've added an if statement that emphasizes the special condition of an empty string.

#include "UpperCaseFilter.h"
#include "ReadFilteredWords.h"
#include "ReadWords.h"
#include <locale>

bool UpperCaseFilter::filter(const string &word) {
  if(word.empty()) //You'll need to do the right thing here
    return true;

  //Even if the right thing to do were to return true, so that
  //the check above would be redundant, you'd want to leave a
  //comment here pointing out that you've handled the special case

  for(size_t i = 0; i < word.length(); i++)
    if(!isupper(static_cast<unsigned char>(word[i])))
      return false;

  return true;
}

Note that your previous function signature was:

bool UpperCaseFilter::filter(string word) {

I've changed this to:

bool UpperCaseFilter::filter(const string &word) {

The const guarantees that the function will not alter word and the & symbol passes the string to the function without copying it. This makes the function faster and saves memory.

#include "UpperCaseFilter.h"
#include "ReadFilteredWords.h"
#include "ReadWords.h"
#include <locale>

bool UpperCaseFilter::filter(string word) 

 {
    int k=0;
    if(!word.empty()) 
     {
        for(int i = 0; i < word.length(); i++) 
          {
            if(isupper(word[i]))
                k++;

           }
       }

   if(k==word.length())
      return true;

   else
      return false; //this will return false even when word length is 0

}


its more simple now provided if you have done other things right this would run.
#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
bool IsAllUpperString(string str)
{
    if(boost::to_upper_copy(str)== str) return true;
    return false;
}
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    string str;
    cin >> str;
    bool flag = false;
    int i = 0;
    while(str[i])
    {
        if(isupper(str[i]))
        { 
            flag = true;
        }
        if(!(isupper(str[i])))
        {
            flag = false;
            break;
        }
        i++;
    }
    if(flag == false)
        cout << "false"  << endl;
    else
        cout << "true" << endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM