检查字符串是否仅包含字母

时间:2019-09-21 03:24:23

标签: c++

我在搜索字符串时遇到麻烦,如果输入仅包含字母的单词,它可以按需工作,但是代码可以工作,但是当我在工作中添加数字时,代码也可以工作问题是,只有当我的var仅包含字母,并且能够找到数字或符号时,才能接受代码开头吗?


#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    label:
    string var1 = "";
cout << "Enter a word: ";
cin >> var1;
    for (int i = 0; i < var1.size (); i++)
    {
        int uppercaseCHar = toupper (var1[i]);
        if (uppercaseCHar < 'A' || uppercaseCHar > 'Z')
        {
            goto endloop;
            cout << endl;

        } else
            goto label;
            cout << endl;

       }
     endloop:
     cout << "Yout word contains only letters";

    }


Output: 
Enter a word: work
Enter a word: wro1
Enter a word: 123
Yout word contains only letters

5 个答案:

答案 0 :(得分:2)

以下是使用std::all_of的解决方案:

#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

int main()
{
    bool isAllLetters = false;
    do 
    {
        std::string var1;
        std::cout << "Enter a word: ";
        std::cin >> var1;

        // check if all characters are letters
        isAllLetters = std::all_of(var1.begin(), var1.end(), [](char ch) 
                                  { return std::isalpha(static_cast<unsigned char>(ch));});

        if ( isAllLetters )
           std::cout << "Your word contains only letters\n";
        else
           std::cout << "Your word contains stuff other than letters\n"
    } while ( !isAllLetters );
}

答案 1 :(得分:2)

简单高效(C ++ 17):

#include <algorithm>
#include <cctype>
#include <string_view>

bool all_of_alpha(std::string_view s)
{
  return std::all_of(s.begin(), s.end(), [](unsigned char c) { return std::isalpha(c); });
}

答案 2 :(得分:1)

避免使用goto,因为它会使大型程序中的调试代码复杂化,这是另一种方式:

int main()
{
    while (true) {
        bool flag = false;  // to check for numeric entry

        string var1; // not req to initialize
        cout << "Enter a word (press exit to end): ";
        cin >> var1;

        for (int i = 0; i < var1.size(); i++)
        {
            int uppercaseCHar = toupper(var1[i]);
            if (!std::isalpha(uppercaseCHar))
            {
                flag = true;
                break;
            }
        }

        if (var1.compare("exit") == 0) break;

        if (flag) {
            cout << "Your word contains number";
            cout << endl;
        }
        else
       {
            cout << "Your word contains only alphabets";
            cout << endl;
       }
    }
}

答案 3 :(得分:0)

允许我提供更现代的惯用语言版本,并且假设您只关心检测ASCII字符集中的字母字符

#include <iostream>
#include <algorithm>
#include <string>

int main()
{
    std::string a = "abcdefgh";
    std::string b = "abcd3fgh";
    std::string c = "abcdefg0";

    auto is_alpha = [](unsigned char c){ return std::isalpha(c); };

    bool aContainsAlphas = std::all_of(a.begin(), a.end(), is_alpha);
    bool bContainsAlphas = std::all_of(b.begin(), b.end(), is_alpha);
    bool cContainsAlphas = std::all_of(c.begin(), c.end(), is_alpha);

    std::cout << std::boolalpha;
    std::cout << "A: " << aContainsAlphas << '\n';    // true
    std::cout << "B: " << bContainsAlphas << '\n';    // false
    std::cout << "C: " << cContainsAlphas << '\n';    // false
}

这定义了一个lambda,它作为参数传递给std::all_of,使用它来测试每个字符:

auto is_alpha = [](unsigned char c){ return std::isalpha(c); };

这将在is_alphaa.begin()之间的每个元素上调用a.end()。如果到达末尾而is_alpha没有任何不匹配,则返回true

bool aContainsAlphas = std::all_of(a.begin(), a.end(), is_alpha) != a.end();

答案 4 :(得分:0)

您还可以使用正则表达式来进一步简化代码。

#include <iostream>
#include <string>
#include <regex>
using namespace std;


int main () {

    regex reg_obj("^[A-Z]+$");     
    string var;
    start_label:
        cout << "Enter a word: ";
        getline(cin, var);
        if(regex_match(var, reg_obj))
            goto endlabel;
        else goto start_label;
    endlabel:
        cout << "your word contains only letters\n";

return 0;

};