character - understanding letter or digit function check in C -


what functions check?

from understand, supposed check if word contains non-alphanumeric character. don't understand how this.

my understanding:

  • the first check length - ok.
  • the second check if character letter:
  • isletter(symbol[0])) evaluates false. logically negated.
  • the third function same above.

what didn't understand, fourth one: isletterordigit(*symbol)).

how check if word has non-alphanumeric characters?

the code:

int issymbolvalid(char* symbol) {     int len = strlen(symbol);      if ((len == 0) || (len > max_symbol_size))     {         strcpy(lastparsingerror, "invalid symbol length");         return 0;     }      if (!isletter(symbol[0]))     {         strcpy(lastparsingerror, "symbol name has start letter");         return 0;     }      while (*symbol != 0)     {         if (isletterordigit(*symbol))         {             strcpy(lastparsingerror, "symbol name can contain letters , digits");             return 0;         }         ++symbol;     }      return 1; }  int isletter(char ch) {     return (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'a') && (ch <= 'z'))); }  int isdigit(char ch) {     return ((ch >= '0') && (ch <= '9')); }  int isletterordigit(char ch) {     return (isletter(ch) && isdigit(ch)); } 

your confusion comes fact function indeed wrong:

int isletterordigit(char ch) {     return (isletter(ch) && isdigit(ch)); } 

&& shows logical and, while character cannot both digit , letter. should have used || (logical or):

int isletterordigit(char ch) {     return (isletter(ch) || isdigit(ch)); } 

while @ it, seems there bug being used:

if (isletterordigit(*symbol)) {     strcpy(lastparsingerror, "symbol name can contain letters , digits");     return 0; } 

you'd want error if characters not letter or digit. therefore (notice !):

if (!isletterordigit(*symbol)) {     strcpy(lastparsingerror, "symbol name can contain letters , digits");     return 0; } 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -