Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中的字符串大写函数+;_C++_String_Function - Fatal编程技术网

C++ c+中的字符串大写函数+;

C++ c+中的字符串大写函数+;,c++,string,function,C++,String,Function,我需要用c语言做两个基本的函数++ bool validMeno(const string& text){ if () return DUMMY_BOOL; } bool validPriezvisko(const string& text){ return DUMMY_BOOL; } 如果输入是第一个字母大写的字符串,则第一个返回true 当所有字符串都是大写时,第二个为true 请帮助我,我是c++中的noob使用header-cctype,这应该

我需要用c语言做两个基本的函数++

bool validMeno(const string& text){
    if ()
    return DUMMY_BOOL;
}

bool validPriezvisko(const string& text){
    return DUMMY_BOOL;
}
如果输入是第一个字母大写的字符串,则第一个返回true 当所有字符串都是大写时,第二个为true
请帮助我,我是c++中的noob

使用header-cctype,这应该可以工作

bool validMeno(const string& text){
    if (text.size() == 0)
        return false;

    return isupper(text[0]);
}

bool validPriezvisko(const string& text){
    if (text.size() == 0)
        return false;

    for (std::string::size_type i=0; i<text.size(); ++i)
    {
        if (islower(text[i]))
            return false;
    }

    return true;
}
bool validMeno(常量字符串和文本){
如果(text.size()==0)
返回false;
返回isupper(文本[0]);
}
bool validPriezvisko(常量字符串和文本){
如果(text.size()==0)
返回false;

对于(STD::SiZeYType I=0;i关系式操作符<代码>=< /COD>为C++类<代码> STD::String 定义。最好的ToupPulExcel实现似乎来自Boost String算法库(参见),所以我将在我的例子中使用它。
#include <boost/algorithm/string.hpp>
#include <string>

bool first_n_upper(const std::string & text, size_t n)
{
    if (text.empty())
        return false;

    std::string upper = boost::to_upper_copy(text);
    return text.substr(0, n) == upper.substr(0, n);
}

bool validMeno(const std::string & text)
{
    return first_n_upper(text, 1);
}

bool validPriezvisko(const std::string & text)
{
    return first_n_upper(text, std::string::npos);
}
#包括
#包括
bool first\u n\u upper(常量标准::字符串和文本,大小)
{
if(text.empty())
返回false;
std::string upper=boost::to_upper_copy(文本);
返回text.substr(0,n)=上限.substr(0,n);
}
bool validMeno(const std::string和text)
{
返回第一个大写字母(文本,1);
}
bool validPriezvisko(const std::string&text)
{
返回第一个字符(文本,std::string::npos);
}

您的问题没有完全指定。非字母字符应该被视为小写还是大写?如果它们应该被视为大写,您可以使用:

#include <string>
#include <algorithm>

using namespace std;

bool validMeno(const string& text) {
    return !text.empty() && isupper(text[0]);
}

bool validPriezvisko(const string& text) {
    return !any_of(begin(text), end(text), islower);
}
#包括
#包括
使用名称空间std;
bool validMeno(常量字符串和文本){
return!text.empty()&&isupper(text[0]);
}
bool validPriezvisko(常量字符串和文本){
return!任意(begin(text)、end(text)、islower);
}
另一方面,如果非字母字符应视为小写:

#include <string>
#include <algorithm>

using namespace std;

bool validMeno(const string& text) {
    return !text.empty() && !islower(text[0]);
}

bool validPriezvisko(const string& text) {
    return all_of(begin(text), end(text), isupper);
}
#包括
#包括
使用名称空间std;
bool validMeno(常量字符串和文本){
return!text.empty()&&!islower(text[0]);
}
bool validPriezvisko(常量字符串和文本){
返回(开始(文本)、结束(文本)、isupper)的所有值;
}

谢谢,这很好,但是我需要一个条件,即名称只包含像a-z a-z这样的普通名称字符