1

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?

#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;
}
  • 4
    What about Lol123, loL123, lOl123 ... – rullof Jan 22 '14 at 16:43
  • Based on the prints in the code, those would all be considered not uppercase. The asker seems to care if a word is all upper case or not. – StephenTG Jan 22 '14 at 16:44
  • 1
    Your if statement `if (isupper(input) == 1) ...` is not correct. The `is...` functions (and most true/false functions in general) may return any nonzero value to indicate truth. The correct way is `if (isupper(foo)) { ... } ` or `if (!isupper(foo)) { ... } ` – Brandin Jan 22 '14 at 16:45
  • @rullof LOl123 should be considered not all uppercase. –  Jan 22 '14 at 16:48
  • The name `isupper` is already used by the standard function, that you are trying to use inside your own code. Since C has no function overloading this can never work. If you are compiling your code with a decent level of warnings, your compiler should cry at you. – Jens Gustedt Jan 22 '14 at 16:50
  • The `ctype.h` api also all take `int` as a formal parameter, so unless you've no plans for dealing with high-asci (and torque the europeans in the process), you should be casting those `char` values to `unsigned int` to avoid a sign-extension. – WhozCraig Jan 22 '14 at 16:50
  • @Brandin the function will not return 1 even if I set it to 'if(d!=0)', d=1, and return d? –  Jan 22 '14 at 16:55
  • @honmayanranger Delete your isupper definition and use the one in ctype. If you want to make your own version, name it something else like `my_isupper` – Brandin Jan 22 '14 at 16:57

5 Answers5

3

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

Live Demo: 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;
}
rullof
  • 7,124
  • 6
  • 27
  • 36
  • Note: The standard makes no warrants about the *sequential* property of characters numeric-eval *except* `'0'...'9'`. While this will "work" on ascii platforms (and even EBCDIC) it isn't by the standard. – WhozCraig Jan 22 '14 at 17:15
  • So, there is no way of finding if an input is all uppercase or lowercase using only isalpha(), islower(), and isupper()? –  Jan 22 '14 at 17:29
2
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++;

}
Malik Fassi
  • 488
  • 1
  • 10
  • 25
0
char c = ...;
if (isalpha(c))
{ 
     // do stuff if it's alpha
} else {
     // do stuff when not alpha
}
atk
  • 9,244
  • 3
  • 32
  • 32
0

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 for loop, so the result that you return is not at all what you think.

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 isupper to be a logical value. Testing that again with ==1 makes not much sense.
  • You have two different variables called input, one in file scope, one in main.
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
0

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; 
}
John Bode
  • 119,563
  • 19
  • 122
  • 198