[英]How to check if a string is certain amount of characters and contains (or doesn't contain) certain characters? C++
Let's say I have two strings (string1, string2) based off of user inputs. 假设我有两个基于用户输入的字符串(string1,string2)。 I want string1 to be at least 5 characters , at most 10 characters , and only allowed to contain the following: 我希望string1 至少为5个字符 , 最多为10个字符 ,并且只能包含以下内容:
Otherwise, the user will be prompted to keep entering strings until the requirements are met. 否则,将提示用户继续输入字符串,直到满足要求为止。
while(len(string1)<5 or len(string1)>10) {
if(len(string1) > 10) {
cout << "Please enter a string that is less than 10 characters: ";
cin >> input;
} else if(len(string) < 5) {
cout << "Please enter a string that is more than 5 characters: ";
cin >> input;
} else {
cout << "Please enter a string with legal characters (uppercase/lowercase letters and numbers): ";
cin >> input;
}
How would I check if string1 only contains uppercase letters, lowercase letters, and numbers? 如何检查string1是否仅包含大写字母,小写字母和数字? Would I use the following in some sort of way?... 我会以某种方式使用以下内容吗?...
string legalChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
string1.find_first_not_of(legalChars)
As for string2, I want the user input to be at least 6 characters long and to contain at least one non-letter character . 至于string2,我希望用户输入的长度至少为6个字符,并且至少包含一个非字母字符 。
while(len(string2)<6) {
if(len(string2)<6) {
cout << "Please enter a string that is at least 6 characters long: ";
cin >> input2;
}
}
How would I check for non-letter characters in string2? 如何检查string2中的非字母字符? How should I approach this problem? 我应该如何解决这个问题?
For string1
you can simply check for the length to be at least 5 characters and at most 10 characters (use std::string::length
), and pass each character into isalnum
to check if it is alphanumeric. 对于string1
您可以简单地检查长度至少为5个字符,最多为10个字符(使用std::string::length
),然后将每个字符传递给isalnum
以检查其是否为字母数字。
For string2
you can check that it's at least 6 characters long and has at least one uppercase letter by using isupper
. 对于string2
您可以使用isupper
来检查其长度是否至少为6个字符,并且至少具有一个大写字母。
Or as @Lorehead pointed out, you can just use regex. 或者就像@Lorehead指出的那样,您可以只使用正则表达式。 Which is better is entirely up to you. 哪个更好,完全取决于您。
Use std::regex_match()
. 使用std::regex_match()
。 ( http://en.cppreference.com/w/cpp/regex/regex_match ). ( http://en.cppreference.com/w/cpp/regex/regex_match )。 No complete answer because this sounds like homework. 没有完整的答案,因为这听起来像是作业。
If you have to do it the old-fashioned way, you can use the functions isalnum()
from <ctype.h>
and strspn()
from <string.h>
. 如果必须采用老式方法,则可以使用<ctype.h>
isalnum()
<ctype.h>
和<string.h>
strspn()
<string.h>
。
Hope this solves your Question....... Here is the link to ascii chart ASCII Chart 希望这能解决您的问题。.......这是ascii图表ASCII图表的链接
#include<iostream>
#include<string>
using namespace std;
int main()
{
string input1,input2;
bool inpFlag1=false,inpFlag2=false;
while(inpFlag1 == false)
{
cout<<"Enter First String:"<<endl;
cin>>input1;
if(input1.size()<4 || input1.size()>10)
{
cout<<"Error!Min:5 Characters and Max:10 characters!"<<endl;
}
else
{
for(int i=0;i<input1.size();i++)
{
if((input1.at(i)>= 48 && input1.at(i)<=57) ||(input1.at(i)>=65 && input1.at(i)<=90) || (input1.at(i)>=97 && input1.at(i)<= 122 ))
{
continue;
}
else
{
cout<<"Error Input string does not match conditions!"<<endl;
inpFlag1=false;
break;
}
}
inpFlag1=true;
}
}
while(inpFlag2 == false)
{
cout<<"Enter Second String:"<<endl;
cin>>input2;
if(input2.size()<6)
{
cout<<"Error! String should contain Min:6 Characters"<<endl;
}
else
{
for(int i=0;i<input2.size();i++)
{
if(input2.at(i)>=33 && input2.at(i)<=126)
{
continue;
}
else
{
cout<<"Error Input String does not match required conditions!" <<endl;
inpFlag2=false;
break;
}
}
inpFlag2=true;
}
}
cout<<endl;
cout<<"Entered Strings:"<<endl;
cout<<input1<<endl;
cout<<input2<<endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.