C语言 检查字母数字中的字符是大写还是小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21288752/
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 10:40:03  来源:igfitidea点击:

checking if character is upper or lower case in alphanumeric

ccharuppercase

提问by Malik Fassi

I have this C code. If I input a LOL123 it should display that it is uppercase. And lol123 it is in lowercase. How do I use isalpha in excluding non-numerical input when checking isupper or is lower?

我有这个 C 代码。如果我输入 LOL123,它应该显示它是大写的。而 lol123 它是小写的。在检查 isupper 或 islower 时如何使用 isalpha 排除非数字输入?

#include <stdio.h>

#define SIZE 6
char input[50];
int my_isupper(char string[]);

int main(){
    char input[] = "LOL123";
    int m;

    m= isupper(input);
    if( m==1){
        printf("%s is all uppercase.\n", input);
    }else
        printf("%s is not all uppercase.\n", input);

    return 0;
}

int my_isupper(char string[]){
    int a,d;

    for (a=0; a<SIZE); a++){
        d= isupper(string[a]) ; 
    }

    if(d != 0)
        d=1;

    return d;
}

回答by rullof

For upper-case function just loop trough the string and if a lowercase character is encountred you return falselike value. And don't use standard library functions names to name your own functions. Use isUpperCaseinstead.

对于大写函数,只需循环遍历字符串,如果遇到小写字符,则返回false类似值。并且不要使用标准库函数名称来命名您自己的函数。使用isUpperCase来代替。

Live Demo: https://eval.in/93429

现场演示:https: //eval.in/93429

#include <stdio.h>
#include <string.h>

int isUpperCase(const char *inputString);

int main(void)
{
    char inputString1[] = "LOL123";
    char inputString2[] = "lol123";
    printf("%s is %s\n", inputString1, isUpperCase(inputString1)?"upper-case":"not upper-case");
    printf("%s is %s\n", inputString2, isUpperCase(inputString2)?"lower-case":"not upper-case");
    return 0;
}

int isUpperCase(const char *inputString)
{
    int i;
    int len = strlen(inputString);
    for (i = 0; i < len; i++) {
        if (inputString[i] >= 'a' && inputString[i] <= 'z') {
            return 0;
        }
    }
    return 1;
}

回答by Malik Fassi

int my_isalpha_lower(int c) {
    return ((c >= 'a' && c <= 'z')); } 

int my_isalpha_upper(int c) {
        return ((c >= 'A' && c <= 'Z')); } 

int isdigit(int c) {
        return (c >= '0' && c <= '9'); }



while (*s) {

     if (!is_digit(*s) && !my_isalpha_lower(*s)) 
     {
         //isnot lower but is alpha 
     }
     else if (!is_digit(*s) && !my_alpha_upper(*s))
     {
        //is not upper but is alpha 
     }

     s++;

}

回答by atk

char c = ...;
if (isalpha(c))
{ 
     // do stuff if it's alpha
} else {
     // do stuff when not alpha
}

回答by Jens Gustedt

You have a lot to learn, besides using a name of a standard function your design also is completely flawed. You only memorize the case of the last character that you encounter in your forloop, so the result that you return is not at all what you think.

你有很多东西要学,除了使用标准函数的名称之外,你的设计也完全有缺陷。您只记住在for循环中遇到的最后一个字符的大小写,因此您返回的结果与您的想法完全不同。

Some more observations:

还有一些观察:

  • Don't use the name of a standard function for your own.
  • Arrays decay to pointers when then are used as function parameters. You have no way to automatically detect the size of the array.
  • You expect your return from isupperto be a logical value. Testing that again with ==1makes not much sense.
  • You have two different variables called input, one in file scope, one in main.
  • 不要为您自己使用标准函数的名称。
  • 数组在用作函数参数时衰减为指针。您无法自动检测数组的大小。
  • 你期望你的回报isupper是一个合乎逻辑的值。再次测试==1没有多大意义。
  • 您有两个不同的变量,称为input,一个在文件范围内,一个在main.

回答by John Bode

Fairly simple:

相当简单:

#include <ctype.h>

/**
 * Will return true if there's at least one alpha character in 
 * the input string *and* all alpha characters are uppercase.
 */
int allUpper( const char *str )
{
  int foundAlpha = 0;                   
  int upper = 1;

  for ( const char *p = str; *p; p++ )
  {
    int alpha = isalpha( *p );           
    foundAlpha = foundAlpha || alpha;    
    if ( alpha )
      upper = upper && isupper( *p );    
  } 

  return foundAlpha && upper; 
}