C++ 检查整个字符串是否为大写
C++ Check if whole string is uppercase
我正在尝试检查整个单词是否为大写,如果为真,则应该 return 为真,否则 return 为假。
我当前的代码是:
#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
,它将 return 为真,因为我的最后一个字符为真。如果整个字符串为真,我怎么会只 return 为真。我使用计数器方法完成了它,但它不是最有效的。
我也尝试使用 all_of
方法,但我认为我没有正确的编译器版本,因为它说 all_of isn't defined
(即使导入正确)。
我不确定还有什么其他方法可以解决这个问题。
你的循环中不应该有两个 return 条件。相反,您可以使用循环来查找问题,如果没有问题,您将退出循环并可以告诉用户最后一切正常。
评论里你说"i believe it doesn't need to return anything if the string is empty";但是,具有 return 类型的函数,例如这个 always returns 东西。如果您不指定 return 值,无论您喜欢与否,它都会给您一个值。因此,您必须决定每个可能的输入的输出应该是什么。因此,我添加了一个 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
而 &
符号将字符串传递给函数而不复制它。这使函数更快并节省内存。
或者在谓词中使用 std::all_of function in combination with std::isupper:
#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;
}
我假设,如果字符串为空,则可以认为是全大写。
#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;
}
#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;
}
我正在尝试检查整个单词是否为大写,如果为真,则应该 return 为真,否则 return 为假。
我当前的代码是:
#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
,它将 return 为真,因为我的最后一个字符为真。如果整个字符串为真,我怎么会只 return 为真。我使用计数器方法完成了它,但它不是最有效的。
我也尝试使用 all_of
方法,但我认为我没有正确的编译器版本,因为它说 all_of isn't defined
(即使导入正确)。
我不确定还有什么其他方法可以解决这个问题。
你的循环中不应该有两个 return 条件。相反,您可以使用循环来查找问题,如果没有问题,您将退出循环并可以告诉用户最后一切正常。
评论里你说"i believe it doesn't need to return anything if the string is empty";但是,具有 return 类型的函数,例如这个 always returns 东西。如果您不指定 return 值,无论您喜欢与否,它都会给您一个值。因此,您必须决定每个可能的输入的输出应该是什么。因此,我添加了一个 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
而 &
符号将字符串传递给函数而不复制它。这使函数更快并节省内存。
或者在谓词中使用 std::all_of function in combination with std::isupper:
#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;
}
我假设,如果字符串为空,则可以认为是全大写。
#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;
}
#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;
}