Thread: is their a better way of checking for alpha?

  1. #1
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222

    is their a better way of checking for alpha?

    Is this a proper way to test if a char is an alpha or am I dreaming. If it even works I feel like I might be doing something the hard way when there is something easier.

    Code:
     #define LOWEST_ALPHA 0x41
     #define HIGHEST_ALPHA 0x5A
     #define LOWEST_CAPS_ALPHA 0x61
     #define HIGHEST_CAPS_ALPHA 0x7A
     
     /* 0000 0000 0000 0000 0000 0000 0111 1111 */
     #define NON_ASCII_BIT_MASK 0x7F
     
     /* 1000 0000 0000 0000 0000 0000 0000 0000 */
     #define ASCII_SIGNED_BIT 0x80000000
     
     /* Clear non ascii bits then set the signed bit */
     #define MAKE_SIGNED_ASCII( ch ) \
     	( ((ch) & NON_ASCII_BIT_MASK ) | ASCII_SIGNED_BIT )
     
     
     #define IN_ASCII_ALPHA_RANGE( ch ) \
     (( ch >= LOWEST_ALPHA && ch <= HIGHEST_ALPHA ) || \
     ( ch >= LOWEST_CAPS_ALPHA && ch <= HIGHEST_CAPS_ALPHA ))
     
     int isalpha( char ch )
     {
     	int result;
     
     	/* Clear non ascii bits then set the signed bit */
     	if ( IN_ASCII_ALPHA_RANGE( MAKE_SIGNED_ASCII( ch ) ){
     		return true;
     	} else {
     		return false;
     	}
     }
    NB: I know of the BSD isalpha() function but my code has to compile on windoze as well.
    "Assumptions are the mother of all **** ups!"

  2. #2
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    isn't isalpha() a standard function -> i guess ctype.h

  3. #3
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    i thought it was *nix only?
    "Assumptions are the mother of all **** ups!"

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    isaplha DOES compiler on windows. Just include the header file "ctype.h"

    edit : run this code and see if it works :

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    {
    char ch;
    
    printf("enter a character :");
    scanf("%c", &ch);
    
    if(isalpha(ch))
       printf("input is alpha\n");
    else
       printf("input is not alpha\n");
    
      return 0;
    }
    Last edited by Brain Cell; 07-16-2004 at 07:53 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    i'm fairly certain it is standard, i've never used a compiler without it in any case
    and in the worst case, you can just write isalpha yourself
    don't settle for ugly macros
    if you do, don't use hex, just use 'literals'

    e;f,b
    .sect signature

  6. #6
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    oh ok, well thanks guys, i feel a bit silly now but it is good to know, ggs please expand on how it would be done with 'literals'?
    "Assumptions are the mother of all **** ups!"

  7. #7
    .
    Join Date
    Nov 2003
    Posts
    307
    FWIW - the isxxxx functions or macros
    Here is an edited part of how an old C compiler did it:

    create an array called _ctype[]:
    Code:
    static char _ctype[256];
    static char _ctype_exists=0;
    void _define__ctype(void){
      int i=0;
      if(_ctype_exists==1) 
             return;
      _ctype_exists=1;
      for(i=129;i<256;i++)
             _ctype[i]=0;
      _ctype[0] = 0; 
      _ctype[1] = 32; 
      _ctype[2] = 32; 
      _ctype[3] = 32; 
      _ctype[4] = 32; 
      _ctype[5] = 32; 
      _ctype[6] = 32; 
      _ctype[7] = 32; 
      _ctype[8] = 32; 
      _ctype[9] = 32; 
      _ctype[10] = 33; 
      _ctype[11] = 33; 
      _ctype[12] = 33; 
      _ctype[13] = 33; 
      _ctype[14] = 33; 
      _ctype[15] = 32; 
      _ctype[16] = 32; 
      _ctype[17] = 32; 
      _ctype[18] = 32; 
      _ctype[19] = 32; 
      _ctype[20] = 32; 
      _ctype[21] = 32; 
      _ctype[22] = 32; 
      _ctype[23] = 32; 
      _ctype[24] = 32; 
      _ctype[25] = 32; 
      _ctype[26] = 32; 
      _ctype[27] = 32; 
      _ctype[28] = 32; 
      _ctype[29] = 32; 
      _ctype[30] = 32; 
      _ctype[31] = 32; 
      _ctype[32] = 32; 
      _ctype[33] = 1; 
      _ctype[34] = 64; 
      _ctype[35] = 64; 
      _ctype[36] = 64; 
      _ctype[37] = 64; 
      _ctype[38] = 64; 
      _ctype[39] = 64; 
      _ctype[40] = 64; 
      _ctype[41] = 64; 
      _ctype[42] = 64; 
      _ctype[43] = 64; 
      _ctype[44] = 64; 
      _ctype[45] = 64; 
      _ctype[46] = 64; 
      _ctype[47] = 64; 
      _ctype[48] = 64; 
      _ctype[49] = 2; 
      _ctype[50] = 2; 
      _ctype[51] = 2; 
      _ctype[52] = 2; 
      _ctype[53] = 2; 
      _ctype[54] = 2; 
      _ctype[55] = 2; 
      _ctype[56] = 2; 
      _ctype[57] = 2; 
      _ctype[58] = 2; 
      _ctype[59] = 64; 
      _ctype[60] = 64; 
      _ctype[61] = 64; 
      _ctype[62] = 64; 
      _ctype[63] = 64; 
      _ctype[64] = 64; 
      _ctype[65] = 64; 
      _ctype[66] = 20; 
      _ctype[67] = 20; 
      _ctype[68] = 20; 
      _ctype[69] = 20; 
      _ctype[70] = 20; 
      _ctype[71] = 20; 
      _ctype[72] = 4; 
      _ctype[73] = 4; 
      _ctype[74] = 4; 
      _ctype[75] = 4; 
      _ctype[76] = 4; 
      _ctype[77] = 4; 
      _ctype[78] = 4; 
      _ctype[79] = 4; 
      _ctype[80] = 4; 
      _ctype[81] = 4; 
      _ctype[82] = 4; 
      _ctype[83] = 4; 
      _ctype[84] = 4; 
      _ctype[85] = 4; 
      _ctype[86] = 4; 
      _ctype[87] = 4; 
      _ctype[88] = 4; 
      _ctype[89] = 4; 
      _ctype[90] = 4; 
      _ctype[91] = 4; 
      _ctype[92] = 64; 
      _ctype[93] = 64; 
      _ctype[94] = 64; 
      _ctype[95] = 64; 
      _ctype[96] = 64; 
      _ctype[97] = 64; 
      _ctype[98] = 24; 
      _ctype[99] = 24; 
      _ctype[100] = 24; 
      _ctype[101] = 24; 
      _ctype[102] = 24; 
      _ctype[103] = 24; 
      _ctype[104] = 8; 
      _ctype[105] = 8; 
      _ctype[106] = 8; 
      _ctype[107] = 8; 
      _ctype[108] = 8; 
      _ctype[109] = 8; 
      _ctype[110] = 8; 
      _ctype[111] = 8; 
      _ctype[112] = 8; 
      _ctype[113] = 8; 
      _ctype[114] = 8; 
      _ctype[115] = 8; 
      _ctype[116] = 8; 
      _ctype[117] = 8; 
      _ctype[118] = 8; 
      _ctype[119] = 8; 
      _ctype[120] = 8; 
      _ctype[121] = 8; 
      _ctype[122] = 8; 
      _ctype[123] = 8; 
      _ctype[124] = 64; 
      _ctype[125] = 64; 
      _ctype[126] = 64; 
      _ctype[127] = 64; 
      _ctype[128] = 32; 
     
    }
    Then in ctype.h you have macros that use _ctype[] :
    Code:
    #define _IS_SP	1			/* is space */
    #define _IS_DIG	2			/* is digit indicator */
    #define _IS_UPP	4			/* is upper case */
    #define _IS_LOW	8			/* is lower case */
    #define _IS_HEX	16			/* [A-F or [a-f] */
    #define _IS_CTL	32			/* Control */
    #define _IS_PUN	64			/* punctuation */
    
    extern	char _Cdecl _ctype[];	 /* Character type array */
    
    #define isalnum(c)	(_ctype[(c) + 1] & (_IS_DIG | _IS_UPP | _IS_LOW))
    #define isalpha(c)	(_ctype[(c) + 1] & (_IS_UPP | _IS_LOW))
    #define isascii(c)	((unsigned)(c) < 128)
    #define iscntrl(c)	(_ctype[(c) + 1] & _IS_CTL)
    #define isdigit(c)	(_ctype[(c) + 1] & _IS_DIG)
    #define isgraph(c)	((c) >= 0x21 && (c) <= 0x7e)
    #define islower(c)	(_ctype[(c) + 1] & _IS_LOW)
    #define isprint(c)	((c) >= 0x20 && (c) <= 0x7e)
    #define ispunct(c)	(_ctype[(c) + 1] & _IS_PUN)
    #define isspace(c)	(_ctype[(c) + 1] & _IS_SP)
    #define isupper(c)	(_ctype[(c) + 1] & _IS_UPP)
    #define isxdigit(c)	(_ctype[(c) + 1] & (_IS_DIG | _IS_HEX))

  8. #8
    Quote Originally Posted by Kinasz
    Is this a proper way to test if a char is an alpha or am I dreaming.
    <cut>
    No.

    Include <ctype.h> and use isalpha(). It belongs to the standard C library.

    Have a look to

    http://www.dinkumware.com/refxc.html

    for a complete list and explanations of standard functions
    Emmanuel Delahaye

    "C is a sharp tool"

  9. #9
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    this is really late, but when i said literals i meant instead of using an ugly hex code

    #define LOWEST_ALPHA 0x41

    you could just

    #define LOWEST_ALPHA 'a'

    there's not even that much a need for that many layers of macros, you are likely not developing your application for any nonascii systems. but yeah, isalpha..
    .sect signature

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array bounds checking
    By rohan_ak1 in forum C Programming
    Replies: 2
    Last Post: 08-26-2008, 10:16 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM