2016-10-22 17 views
-4

私はユーザ入力の文字列入力を扱っており、入力した各入力を評価するにはswitch-statementsを使用する必要があります。私のコードは現在、ユーザーの文字列の入力を評価し、ASCIIコードを使用して大文字、数字、または特殊文字かどうかを調べます。 switch文がどのように動作するのか、If文をswitch文に変更する方法を今私は確信しています。文字列入力を評価するswitch文C++

for (int i = 0; i < strlength; i++) //for loop used to check the rules of the password inputted by the user 
{ 
    cout << "Testing for upper case characters..." << endl; //displays the cout 
    tmpi=(int) str1[i]; //stoi function making the string input an integer 
    if ((tmpi >= 65) && (tmpi <= 90)) //checks if there are two upper case characters in the string 
    { 
     cout << "Found an uppercase" << endl; 
     uppercnt++; //adds to the counter of upper case 
     state++; 
     cout << "Now in state q" << state << "..." << endl; 
     continue; 
    } 
    cout << "Testing for digits..." << endl; 
    if(tmpi >= 48 && tmpi <= 57) //checks if there are two digits in the string 
    { 
     cout << "Found a digit" << endl; 
     digitcnt++; //adds to the counter of digit 
     state++; 
     cout << "Now in state q" << state << "..." << endl; 
     continue; 
    } 
    cout << "Testing for special characters..." << endl; 
    if(tmpi >= 33 && tmpi <= 47 || tmpi >= 58 && tmpi <= 64 || tmpi >= 91 && tmpi <= 96 || tmpi >= 123 && tmpi <= 126) //checks if there are special characters 
    { 
     cout << "Found a special char" << endl; 
     speccnt++; //adds to the counter of special character 
     state++; 
     cout << "Now in state q" << state << "..." << endl; 
     continue; 
    } 
    cout << "Character entered was a lower case" << endl; 
    state++; 
    cout << "Now in state q" << state << "..." << endl; 
} //end for loop 

ありがとうございました。ありがとうございました。

+0

int upps = std::count_if(pass.begin(), pass.end(), isupper); int digs = std::count_if(pass.begin(), pass.end(), isdigit); 

例の作業を任意の入力や方向やリンクは、私は非常に多くのスイッチ・ステートメントを使用していない助けるとIだろう彼らがどのように働くか100%確信していません。 – jravager

+0

あなたは本当に[ask]を読むべきです。 – dandan78

+0

@ dandan78初めてクエストを投稿したら、それを改善するために何を変更する必要がありますか? – jravager

答えて

0

もパフォーマンスの問題は、私はちょうどstd::count_if使用しない場合:ideone

+0

この例では、アルゴリズムライブラリの「isupper」部分ですか? – jravager

+0

また、私は特殊文字のためにこれに似た何かをすることができますか? – jravager

+0

ここにすべての機能がありますhttp://en.cppreference.com/w/cpp/string/byte/isalpha – Slava

関連する問題