2

我在这里完全是新手。如上所述,我想知道如何检查输入字符串是否包含大写和小写的组合。之后打印一条语句以显示输入字符串包含大写和小写的组合。提前致谢。

4

7 回答 7

4

步骤 0:您需要的变量

char* str;
int   i;
char  found_lower, found_upper;

第一步:遍历字符串

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')

第三步:合并结果

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);

}
于 2013-03-02T07:05:48.347 回答
3

像这样的东西(可以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;
}
于 2013-03-02T07:20:53.197 回答
1

迭代输入字符串中的每个字符(我假设它是作业并且它是 ASCII)并检查字符是否是小写字母。在这种情况下,将标记是否满足小写字母的变量设置为 true。对大写执行相同的操作(或者您可以在同一个循环中执行此操作)。然后根据两个布尔变量形成您的输出。

于 2013-03-02T07:04:50.440 回答
1
#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;
}
于 2013-03-02T07:14:25.233 回答
0
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);
于 2013-03-02T07:05:04.773 回答
0

您可以使用 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.
于 2013-03-02T07:09:15.327 回答
0

您是否需要返回大小写有差异的地方,或者只是大小写有差异?

您可以将 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 */
}

希望这会有所帮助!

于 2013-03-02T07:10:14.570 回答