【问题标题】:checking if character is upper or lower case in alphanumeric检查字符在字母数字中是大写还是小写
【发布时间】:2014-02-12 20:30:43
【问题描述】:

我有这个 C 代码。如果我输入 LOL123 它应该显示它是大写的。而lol123它是小写的。检查 isupper 或 is lower 时如何使用 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;
}

【问题讨论】:

  • Lol123、loL123、lOl123 怎么样……
  • 根据代码中的打印,这些都将被认为不是大写。提问者似乎很在意单词是否全部大写。
  • a&lt;sizeof(string) 是错误的。你可以改用for (a=0; string[a] ; a++) {
  • 您的 if 语句 if (isupper(input) == 1) ... 不正确。 is... 函数(以及一般的大多数真/假函数)可以返回任何非零值来指示真值。正确的方式是if (isupper(foo)) { ... } if (!isupper(foo)) { ... }
  • @rullof LOl123 不应该全部大写。

标签: c char uppercase


【解决方案1】:
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++;

}

【讨论】:

    【解决方案2】:

    相当简单:

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

    【讨论】:

      【解决方案3】:

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

      现场演示: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;
      }
      

      【讨论】:

      • 注意:该标准不保证字符 numeric-eval except '0'...'9'sequential 属性。虽然这将在 ascii 平台(甚至 EBCDIC)上“工作”,但它不符合标准。
      • 那么,仅使用 isalpha()、islower() 和 isupper() 就无法判断输入是全大写还是小写?
      【解决方案4】:

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

      更多观察:

      • 不要为自己使用标准函数的名称。
      • 数组在用作函数参数时衰减为指针。您无法自动检测数组的大小。
      • 您希望从isupper 返回的返回值是一个合乎逻辑的值。用==1 再次测试没有多大意义。
      • 您有两个不同的变量,称为input,一个在文件范围内,一个在main

      【讨论】:

        【解决方案5】:
        char c = ...;
        if (isalpha(c))
        { 
             // do stuff if it's alpha
        } else {
             // do stuff when not alpha
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-06-17
          • 1970-01-01
          • 2015-04-23
          • 2011-02-18
          • 2021-08-12
          • 2012-01-03
          • 1970-01-01
          • 2020-10-21
          相关资源
          最近更新 更多