ascii string question

This is a discussion on ascii string question within the C Programming forums, part of the General Programming Boards category; how to check that a strings chars are sorted in an ascending order regarding their ascii code and consist only ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    ascii string question

    how to check that a strings chars are sorted in an ascending order regarding their ascii code
    and consist only for this ranges a-z A-Z

    ??

    how to extract the ascii code from each cell??

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    loop through the string, return false if you get to something not in a-z A-Z, or an element smaller than the last. Return true if you reach the end.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    Code:
    //
    // 1 = Upper Char (A-Z)
    // 2 = Lower Char (a-z)
    // 0 -  Not Alpha Char.
    //
    int CheckIsAlpha (char pchar){
    	int iASCIIcode;
    	
    	iASCIIcode = pchar;
    	
    	if (iASCIIcode >= 65 || iASCIIcode <= 90)
    			return 1;
    	
    	if (iASCIIcode >= 97 || iASCIIcode <= 122)
    			return 2;
    			
    	return 0;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sergioms View Post
    Code:
    //
    // 1 = Upper Char (A-Z)
    // 2 = Lower Char (a-z)
    // 0 -  Not Alpha Char.
    //
    int CheckIsAlpha (char pchar){
    	int iASCIIcode;
    	
    	iASCIIcode = pchar;
    	
    	if (iASCIIcode >= 65 || iASCIIcode <= 90)
    			return 1;
    	
    	if (iASCIIcode >= 97 || iASCIIcode <= 122)
    			return 2;
    			
    	return 0;
    }
    Surely this is at least easier to read, even if it doesn't really fix problems with EBCDIC and other non-ASCII characters sets:

    Code:
    	if (iASCIIcode >= 'A' || iASCIIcode <= 'Z')
    			return 1;
    	
    	if (iASCIIcode >= 'a' || iASCIIcode <= 'z')
    			return 2;
    Saves trying to remember what the ascii codes are for different letters.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And what's wrong with isalpha, while we're here?

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by sergioms View Post
    Code:
    //
    // 1 = Upper Char (A-Z)
    // 2 = Lower Char (a-z)
    // 0 -  Not Alpha Char.
    //
    int CheckIsAlpha (char pchar){
    	int iASCIIcode;
    	
    	iASCIIcode = pchar;
    	
    	if (iASCIIcode >= 65 || iASCIIcode <= 90)
    			return 1;
    	
    	if (iASCIIcode >= 97 || iASCIIcode <= 122)
    			return 2;
    			
    	return 0;
    }
    Yuk! Magic numbers.
    Use 'A' instead of 65, 'Z' instead of 90...
    Also, there's no point in saving the char in an int first.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Why all the fuss?

    Code:
    int CheckIsAlpha (char pchar) {	
    	return isalpha(pchar) ? ( islower(pchar) ? 2 : 1  ) :  0 ; 
    }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  3. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21