C语言 C编程:如何检查输入的字符串是否包含大写和小写组合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15171740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:32:12  来源:igfitidea点击:

C programming: How to check whether the input string contains combination of uppercase and lowercase

c

提问by user2126081

I'm totally newbie here. As stated above, I would like to know how to check whether the input string contains combination of uppercase and lowercase. After that print a statement to show that the input string contains combination of uppercase and lowercase. Thanks in advance.

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

回答by slezica

Step 0: variables you need

第 0 步:您需要的变量

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

Step 1: iterate through the string

第一步:遍历字符串

for (int i = 0; str[i] != '
found_lower = found_lower || (str[i] >= 'a' && str[i] <= 'z')
found_upper = found_upper || (str[i] >= 'A' && str[i] <= 'Z')
'; i++)

Step 2: detect upper and lower case characters

第二步:检测大小写字符

mixed_case = found_lower && found_upper

Step 3: combine the results

第 3 步:合并结果

if (found_lower && found_upper) break;

Step 4 (optional) break out of the forearly to save some time

Step 4(可选)for早点突破以节省一些时间

char is_mixed(char* str) {

    int   i;
    char  found_lower = false, found_upper = false;

    for (int i = 0; str[i] != '
#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;
}
'; 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); }

Full source (warning: untested):

完整来源(警告:未经测试):

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

回答by WhozCraig

Something like this (which willwork on both ASCII and EBCDIC platforms):

像这样的东西(在 ASCII 和 EBCDIC 平台上工作):

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

回答by Mudassir Hasan

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.

回答by Drakosha

Iterate every char in the input string (i am assuming it's homework and it's ASCII) and check whether the char is lower case letter. In this case, set to true a variable which marks whether lower case letter was met. Do the same for upper case (or you could do it in the same loop). Then form your output based on the two boolean variables.

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

回答by Tuxdude

 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.

回答by Puran Joshi

You can easily do it using the ASCII value.

您可以使用 ASCII 值轻松完成此操作。

Here are 2 algorithm that you can code:

以下是您可以编码的 2 个算法:

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 */
}

The other way much simpler is.

另一种更简单的方法是。

##代码##

回答by Connor Hollis

Do you need to return where there are differences in case or just whether there is a difference in case or not?

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

You can compare character codes in ASCII to one another to check if your value is within a range or not.

您可以将 ASCII 中的字符代码相互比较,以检查您的值是否在一个范围内。

This code works if you don't know that the string will be only letters. You can remove some of the checks if you know that it will be only letters.

如果您不知道字符串只是字母,则此代码有效。如果您知道它只是字母,则可以删除一些检查。

##代码##

Hopefully that helps!

希望这有帮助!