私はここでまったく初心者です。上記のように、入力文字列に大文字と小文字の組み合わせが含まれているかどうかを確認する方法を知りたいです。その後、入力文字列に大文字と小文字の組み合わせが含まれていることを示すステートメントを出力します。前もって感謝します。
7 に答える
ステップ 0: 必要な変数
char* str;
int i;
char found_lower, found_upper;
ステップ 1: 文字列を反復処理する
for (int i = 0; str[i] != '\0'; i++)
ステップ 2: 大文字と小文字の検出
found_lower = found_lower || (str[i] >= 'a' && str[i] <= 'z')
found_upper = found_upper || (str[i] >= 'A' && str[i] <= 'Z')
ステップ 3: 結果を組み合わせる
mixed_case = found_lower && found_upper
for
ステップ 4 (オプション)時間を節約するために早い段階から抜け出す
if (found_lower && found_upper) break;
完全なソース (警告: 未テスト):
char is_mixed(char* str) {
int i;
char found_lower = false, found_upper = false;
for (int i = 0; str[i] != '\0'; i++) {
found_lower = found_lower || (str[i] >= 'a' && str[i] <= 'z');
found_upper = found_upper || (str[i] >= 'A' && str[i] <= 'Z');
if (found_lower && found_upper) break;
}
return (found_lower && found_upper);
}
このようなもの ( ASCII プラットフォームと EBCDIC プラットフォームの両方で動作します) :
#include <ctype.h>
int hasMixedCase(const char *src)
{
int hasUpper=0, hasLower=0;
for (;*src && !(hasUpper && hasLower);++src)
{
hasUpper = hasUpper || (isalpha(*src) && *src == toupper(*src));
hasLower = hasLower || (isalpha(*src) && *src == tolower(*src));
}
return hasLower && hasUpper;
}
#include <stdio.h>
#include <ctype.h>
int main ()
{
char* str="Test String.\n";
int Uflag=0;
int Lflag=0;
char c;
for (int i=0; i<str.length(); ++i)
{
c=str[i];
if (islower(c))
Lflag=1;
if (isupper(c))
Uflag=1;
if(Lflag!=0 && Uflag!=0)
{
printf("String contains combo of Upper and Lowercase letter");
break; // both upper case and lower case letter found , no need to iterate further.
}
}
return 0;
}
入力文字列内のすべての文字を繰り返し(宿題でASCIIであると想定しています)、文字が小文字かどうかを確認します。この場合、小文字が一致したかどうかを示す変数を true に設定します。大文字についても同じことを行います (または、同じループで行うこともできます)。次に、2 つのブール変数に基づいて出力を形成します。
unsigned int len = strlen(inputStr);
bool containsUpperCase = false;
bool containsLowerCase = false;
for (int i = 0; i < len && !(containsUpperCase && containsLowerCase); ++i)
{
char c = inputStr[i];
if (c >= 'A' && c <= 'Z')
containsUpperCase = true;
else if (c >= 'a' && c <= 'z'))
containsLowerCase = true;
}
printf("Contains Upper Case: %d Contains Lower Case: %d\n",
containsUpperCase, containsLowerCase);
ASCII 値を使用して簡単に実行できます。
コーディングできるアルゴリズムは次の 2 つです。
1. Intialize two variable lowerCase as false and upperCase as false.
2. Select each character from the input string.
2.a. Get the ascii value for that character
2.b. If greater or equal to 97 then set lowercase as true. else set upper case as true.
3. If end result contains upperCase as well as lowerCase as true than it contains combination of upper and lowercase.
もう一方の方法はもっと簡単です。
1. convert the given string to lowerCase.
2. check if it is equal to actual string if true then it is in lowerCase and return.
3. Convert actual string to upperCase and compare again to actual string
4. If equal than string in upperCase else it is combination of upper and lowercase.
大文字と小文字の違いがある場合は返す必要がありますか、それとも大文字と小文字の違いがあるかどうかだけを返す必要がありますか?
ASCII の文字コードを相互に比較して、値が範囲内にあるかどうかを確認できます。
このコードは、文字列が文字だけになることがわからない場合に機能します。文字だけになることがわかっている場合は、チェックの一部を削除できます。
int checkLowerAndUpper( char * string ) /* pass a null-terminated char pointer */
{
int i; /* loop variable */
int length = strlen(string); /* Length */
int foundLower = 0; /* "boolean" integers */
int foundUpper = 0;
for( i = 0; i < length; ++i ) /* Loop over the entire string */
{
if( string[i] >= 'a' && string[i] <= 'z' ) /* Check for lowercase */
foundLower = 1;
else if( string[i] >= 'A' && string[i] <= 'Z' ) /* Compare uppercase */
foundUpper = 1;
if( foundLower && foundUpper )
return 1; /* There are multi case characters in this string */
}
return 0; /* All of the letters are one case */
}
うまくいけば、それは役に立ちます!