[英]C++ Check if whole string is uppercase
我試圖檢查整個單詞是否為大寫,如果這是真的,它應該返回真,否則返回假。
我目前的代碼是:
#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;
}
}
}
}
這段代碼的問題是,如果我有例如HeLLO
,它將返回 true 因為我的最后一個字符是 true。 如果整個字符串為真,我將如何只返回真。 我是用計數器方法做的,但它不是最有效的。
我也嘗試使用all_of
方法,但我認為我沒有正確的編譯器版本,因為它說all_of isn't defined
(即使導入正確)。
我不確定還有什么其他方法可以解決這個問題。
或者,在謂詞中結合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';
}
}
用作函數的一部分:
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;
}
我假設,如果字符串為空,則可以將其視為全大寫。
你的循環中不應該有兩個返回條件。 相反,您可以使用循環來查找問題,如果沒有問題,您將退出循環並告訴用戶最后一切正常。
在評論中,您說“如果字符串為空,我相信它不需要返回任何內容”; 但是,具有返回類型的函數(例如 this )總是會返回某些內容。 如果您不指定返回值,無論您喜歡與否,它都會給您一個。 因此,您必須決定每個可能的輸入的輸出應該是什么。 因此,我添加了一個 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;
}
請注意,您之前的函數簽名是:
bool UpperCaseFilter::filter(string word) {
我已將其更改為:
bool UpperCaseFilter::filter(const string &word) {
const
保證函數不會改變word
, &
符號將字符串傳遞給函數而不復制它。 這使函數更快並節省內存。
#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.