简体   繁体   English

验证用户输入字符串上的大小写模式(isupper / islower)

[英]Validate case pattern (isupper/islower) on user input string

I need to write a program that checks if the user-provided first and last names are correctly typed. 我需要编写一个程序来检查用户提供的名字和姓氏是否正确键入。 The program needs to validate that only the first letter of each name part is uppercase. 该程序需要验证每个名称部分的仅首字母是大写。

I managed to write code that checks the first character of the input. 我设法编写了代码来检查输入的第一个字符。 So I have a problem when for example "JOHN" is entered. 所以当输入例如“ JOHN”时,我有一个问题。 A correct input would be for example "John Smith". 正确的输入例如是“ John Smith”。

Here's the code: 这是代码:

#include <iostream>
#include <string>

using namespace std;

int main ()
{

  std::string str;
  cout << "Type First Name: ";
  cin >> str;

    if(isupper(str[0]))
    {
        cout << "Correct!" <<endl;
    }

    else
    {
        cout << "Incorrect!" <<endl;
    }


  system("pause");
  return 0;
}

The simplest thing you can do is to use a for/while loop. 您可以做的最简单的事情是使用for / while循环。 A loop will basically repeat the same instruction for a number of n steps or until a certain condition is matched. 循环基本上将相同的指令重复n步,或者直到满足特定条件为止。

The solution provided is pretty dummy, if you want to read the first name and last name at the same time you will have to spit the string via " " delimiter. 所提供的解决方案非常虚构,如果您想同时读取名字和姓氏,则必须通过“”分隔符将字符串吐出。 You can achieve this result using strtok() in C/C++ or with the help of find in C++. 您可以在C / C ++中使用strtok()或在C ++中使用find来获得此结果。 You can see some examples of how to split here . 您可以在此处看到一些如何拆分的示例。

You can easily modify your code to look like this: 您可以轻松地将代码修改为如下所示:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    std::string str;
    std::vector<std::string> data = { "First", "Last" };
    int j;

    for (int i = 0; i < 2; i++) {
        cout << "Type " << data[i] << " Name: ";
        cin >> str;

        if (isupper(str[0])) {

            for (j = 1; j < str.size(); j++) {
                if (!islower(str[j]))
                {
                    cout << "InCorrect!" << endl;
                    break; // Exit the loow
                }
            }
            if(j==str.size())
                cout << "Correct!" << endl;
        }
        else {
            cout << "InCorrect!" << endl;
        }
    }

    system("pause");
    return 0;
}

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

相关问题 wstring的isupper和islower - isupper and islower for wstring 如何在 c++ 中使用 isupper、isdigit 和 regex 验证用户输入密码 - How do I validate user input password using isupper, isdigit and regex in c++ 在这种情况下如何验证用户输入? - How to validate the user input in this case? 字符转换函数 std::isupper() &amp; std::islower() C++17 - Character converting funtion std::isupper() & std::islower() C++17 isupper(),islower(),toupper(),tolower()函数在c ++中不起作用 - the isupper(), islower(), toupper(), tolower() functions not working in c++ islower函数根据输入输出布尔值0或1 - islower function to output boolean value of 0 or 1 depending on input 使用 while 循环验证字符串输入 - validate string input with while loop 如何验证用户输入以匹配不带%,* 、?等字符的任何字符串? 在C ++ 11中 - How To Validate user input to match any string without characters like %, *, ? in C++11 错误:没有匹配函数可调用&#39;isupper(std :: string&)&#39;| - error: no matching function for call to 'isupper(std::string&)'| 将Xaml文本框转换为double,但是如何验证用户输入是否为double,而不是Windows 8应用程序的c ++中的字符串 - Converting Xaml textbox to double, but how to validate that the user input is double not a string in c++ for the windows 8 app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM