简体   繁体   中英

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. I want string1 to be at least 5 characters , at most 10 characters , and only allowed to contain the following:

  1. Upper case letters
  2. Lower case letters
  3. Numbers (0-9)

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? 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 .

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? 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.

For string2 you can check that it's at least 6 characters long and has at least one uppercase letter by using isupper .

Or as @Lorehead pointed out, you can just use regex. Which is better is entirely up to you.

Use std::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> .

Hope this solves your Question....... Here is the link to ascii chart ASCII Chart

#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;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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