简体   繁体   English

C++ 检查整个字符串是否为大写

[英]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.这段代码的问题是,如果我有例如HeLLO ,它将返回 true 因为我的最后一个字符是 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).我也尝试使用all_of方法,但我认为我没有正确的编译器版本,因为它说all_of isn't defined (即使导入正确)。

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:或者,在谓词中结合std::isupper使用std::all_of函数:

#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.但是,具有返回类型的函数(例如 this )总是会返回某些内容。 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.因此,我添加了一个 if 语句来强调空字符串的特殊条件。

#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. const保证函数不会改变word&符号将字符串传递给函数而不复制它。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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