简体   繁体   English

如何仅在C ++中将用户输入限制为数字和字母

[英]How to limit input from user to numbers and letters only in C++

The program only needs numbers and/or letters as input from the user. 该程序仅需要数字和/或字母作为用户输入。 otherwise, the program must be terminated. 否则,该程序必须终止。 I don't know how to limit the input to numbers and letters only. 我不知道如何将输入限制为仅数字和字母。

Here is my program: 这是我的程序:

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

int main()
{
    string input, reversed = "";

    cout << "Enter string: "; 
    cin >> input;

    for (int i = input.length() - 1; i < input.length(); i--)
    {
        reversed += input[i];
    }
    cout << reversed << endl;

    if (reversed == input)  cout << "It is a palindrome!"; 
    else            cout << "No, it is not a palindrome!";
    return 0;
}

The simplest way you can achieve what you want is checking every character. 实现所需内容的最简单方法是检查每个字符。 As @Pete Becker pointed out, you may use isalnum to check if the character is a number or a letter character: 正如@Pete Becker指出的那样,您可以使用isalnum来检查字符是数字还是字母字符:

#include <iostream>
#include <string>

int main () {
  std::string s = "Hello W>orld";

  for (auto c : s) {
    if (!isalnum(c)) {
      std::cout << "Found : '" << c << "'" << std::endl;
    }
  }

  return 0;
}

Output: 输出:

Found : ' '
Found : '>'

As @Someprogrammerdude mentioned in the comments, using std::all_of and an appropriate binary predicate(lambda function), you can do it easily as follows. 正如@Someprogrammerdude在评论中提到的那样,使用std :: all_of和适当的二进制谓词(lambda函数),您可以轻松地做到以下几点。

Secondly, for checking the string input to its reverse string, just create a temporary string using the reverse iterators of std::string and check it. 其次,要检查input到其反向字符串的字符串,只需使用std::string 的反向迭代器创建一个临时std::string并进行检查。 That way, you do not need to have another variable. 这样,您就不需要另一个变量。

Hope the comments will help you to understand more options. 希望这些评论将帮助您了解更多选项。 SEE LIVE 现场直播

#include <iostream>
#include <cctype> // std::isdigit and std::isalpha and std::isalnum
#include <string>
#include <algorithm> // std::all_of

int main()
{
    std::string input; std::cin >> input;

    const auto check = [](const char eachCar)->bool{ return std::isalnum(eachCar); };
    /* change return statement of lambda to
        std::isdigit(eachCar)  ---> for only digits
        std::isalpha(eachCar)  ---> for only letters
    */
    if(std::all_of(input.cbegin(), input.cend(), check))
    {
        if(input == std::string(input.crbegin(), input.crend()))
            std::cout << "It is a palindrome!";
        else  std::cout << "No, it is not a palindrome!";
    }
    return 0;
}

Input : 输入

123kk321

Output : 输出

It is a palindrome!

Assume the input has no space, you can do like this: 假设输入没有空间,可以这样:

char c; string s; bool chk = true;
while (cin >> c) 
{
    if (('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) 
        s.push_back(c);
    else chk = false;
}
if (chk == true) cout << "Valid string";
else cout << "Invalid string";

The above code will terminate when there is no character left in the input. 当输入中没有字符时,以上代码将终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM