2017-10-02 9 views
1

文字列の大文字のみをチェックするために文字列を検証するにはどうすればよいですか?そして、それが大文字と小文字の両方を持っているかどうかをどうやってチェックしますか?あなたはそれが唯一の大文字を持っているかどうかを確認するために、文字列を検証するにはどうすればよい文字列のバリデーション

cout << "Enter state name or state abbr:" << " " << endl; 
cin >> st; 

if ((st == stateArray[in].getStateAbbr()) && (std::all_of(st.begin(), st.end(), isupper))) // uppercase only 
{ 
    cout << "Found. Your string is a state abbr." << endl; 
} 
else if ((st == stateArray[in].getStateName())) 
{ 
    cout << "Found." << " Your string is a state name" << endl; 
} 
else 
{ 
    cout << "Not found" << endl; 
} 
+4

をあなたの質問?それは働いていないのですか?問題が何であっても、短い完全な例を強調表示することをお勧めします。 –

+0

[良い質問をする方法を読む](http://stackoverflow.com/help/how-to-ask)に時間を割いて、[最小限の、完全で検証可能な例](http ://stackoverflow.com/help/mcve)。また、Eric Lippertの[小さなプログラムのデバッグ方法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を読んで、デバッガの使い方を学ぶことをお勧めします。 –

+0

@RetiredNinjaステート名のために大文字と小文字が必要ですが、状態の省略にはすべて大文字である必要があります。 –

答えて

0

?そして、それが大文字と小文字の両方を持っているかどうかをどうやって調べますか?

あなたはASCII文字のみを使用している場合は、あなたが行うことができます。その後、

std::string st; 
getline(std::cin, st); 

と:

//check all uppercase 
bool upperCase = true; 

for (char c : st) 
{ 
    if (c < 65 || c > 90) 
    { 
     upperCase = false; 
     break; 
    } 
} 

//check all lowercase 
bool lowerCase = true; 

for (char c : st) 
{ 
    if (c < 97 || c > 122) 
    { 
     lowerCase = false; 
     break; 
    } 
} 

//check both uppercase and lowercase 
bool both = true; 

for (char c : st) 
{ 
    if ((c < 65) || (c > 90 && c < 97) || (c > 122)) 
    { 
     both = false; 
     break; 
    } 
} 

その後、あなたはブール変数をチェックすることができますは何

if (upperCase) std::cout << "All chars are upper case." << std::endl; 
if (lowerCase) std::cout << "All chars are lower case." << std::endl; 
if (both)  std::cout << "All chars are upper case or lower case." << std::endl; 
+0

の正規表現を使用することができます。文字列に拡張ASCII文字が含まれているとうまくいかない場合は、std :: islowerとstd :: isupperの両方を使用するのはなぜですか? –

関連する問題