Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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++_String_Lowercase_Alphanumeric - Fatal编程技术网

C++ 如何检查字符串是否都是小写和字母数字?

C++ 如何检查字符串是否都是小写和字母数字?,c++,string,lowercase,alphanumeric,C++,String,Lowercase,Alphanumeric,是否有检查这些情况的方法?或者我需要解析字符串中的每个字母,并检查它是否是小写(字母),是否是数字/字母?您可以使用它来检查每个字符的条件。没有字符串级别的函数来执行此操作,因此您必须编写自己的。假设“C”区域设置是可接受的(或为标准换入不同的字符集),请使用首先查找\u not\u of() #包括 bool测试字符串(const std::string和str) { 标准:字符串标准(“abcdefghijklmnopqrstuvwxyz0123456789”); 返回(std::strin

是否有检查这些情况的方法?或者我需要解析字符串中的每个字母,并检查它是否是小写(字母),是否是数字/字母?

您可以使用它来检查每个字符的条件。没有字符串级别的函数来执行此操作,因此您必须编写自己的。

假设“C”区域设置是可接受的(或为
标准换入不同的字符集),请使用
首先查找\u not\u of()

#包括
bool测试字符串(const std::string和str)
{
标准:字符串标准(“abcdefghijklmnopqrstuvwxyz0123456789”);
返回(std::string::npos==str.find_first_not_of(条件);
}

您可以使用tolower&strcmp来比较原始字符串和tolowered字符串。并对每个字符分别进行编号

(或)按以下每个字符执行这两项操作

#include <algorithm>

static inline bool is_not_alphanum_lower(char c)
{
    return (!isalnum(c) || !islower(c));
}

bool string_is_valid(const std::string &str)
{
    return find_if(str.begin(), str.end(), is_not_alphanum_lower) == str.end();
}

#include

如果字符串包含ASCII编码的文本,并且您喜欢编写自己的函数(就像我一样),那么您可以使用以下方法:

bool is_lower_alphanumeric(const string& txt)
{
  for(char c : txt)
  {
    if (!((c >= '0' and c <= '9') or (c >= 'a' and c <= 'z'))) return false;
  }
  return true;
}
bool是较低的字母数字(const string&txt)
{
for(字符c:txt)
{

if(!((c>='0'和c='a'和c这还不是很清楚,但是区域设置实际上有一个函数来确定一次整个字符串的特征。具体地说,区域设置的
ctype
方面有一个
scan\u is
和一个
scan\u not
来扫描符合指定掩码的第一个字符(字母、数字、字母数字、下限、上限、标点符号、空格、十六进制数字等)或不适合的第一个。除此之外,它们的工作方式有点像
std::find_if
,返回作为“end”传递的内容表示失败,否则返回指向字符串中不符合要求的第一项的指针

下面是一个快速示例:

#include <locale>
#include <iostream>
#include <iomanip>

int main() {

    std::string inputs[] = { 
        "alllower",
        "1234",
        "lower132",
        "including a space"
    };

    // We'll use the "classic" (C) locale, but this works with any
    std::locale loc(std::locale::classic());

    // A mask specifying the characters to search for:          
    std::ctype_base::mask m = std::ctype_base::lower | std::ctype_base::digit;

    for (int i=0; i<4; i++) {
        char const *pos;
        char const *b = &*inputs[i].begin();
        char const *e = &*inputs[i].end();

        std::cout << "Input: " << std::setw(20) << inputs[i] << ":\t";

        // finally, call the actual function:
        if ((pos=std::use_facet<std::ctype<char> >(loc).scan_not(m, b, e)) == e)
            std::cout << "All characters match mask\n";
        else
            std::cout << "First non-matching character = \"" << *pos << "\"\n";
    }
    return 0;
}
#包括
#包括
#包括
int main(){
std::字符串输入[]={
“alllower”,
"1234",
“降低132”,
“包括空间”
};
//我们将使用“经典”(C)语言环境,但这适用于任何
std::locale loc(std::locale::classic());
//指定要搜索的字符的掩码:
std::ctype_base::mask m=std::ctype_base::lower | std::ctype_base::digit;
对于(inti=0;i只需使用

如果您不关心语言环境(即输入为纯7位ASCII),则可以将条件优化为

[](const char c){ return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z'); }

[](const char c){return('0'很好的观点。我编辑了这个来反映这一点。我不知道这个函数。我在这里有点困惑,你用“Good point…”注释了你自己的答案@t0mm13b在我发表我的评论之前,有一条评论指出了isalnum().我想它已经被删除了:PNo..在发布问题后,你仍然需要等待x分钟才能接受答案,但是thanks@Oleksi:我完全同意你的被接受——虽然这个函数确实存在,但我不确定我会推荐使用它。我很确定大多数人会发现类似于
islower
或者循环中的isalnum
,或者传递到
std::find
更容易理解。假设语言环境为“C”(这可能对OP不重要)@bgporter对于近两年后的回复表示抱歉,但这种方法的效率有多高?我正在研究一种化学方程式平衡器,这似乎是一种理想的预检查,而不是对每个字符执行多次检查。我想问题是“与什么相比效率高”。我的第一个选择是假设标准库一般都是有效的速度足够快(并且相对没有错误)与手写代码相比。@Andrue它不会非常有效,因为
std::find_first_not_of
需要一个内部循环来检查元素是否不在集合中,这与
isdigit
isalnum
islower
不同,后者可以根据区域设置使用少量比较
bool lowerAlnum = std::all_of(str.cbegin(), str.cend(), [](const char c){
    return isdigit(c) || islower(c);
});
[](const char c){ return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z'); }