Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,924 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,687 people online right now. Registration is fast and FREE... Join Now!





Some simple ASCII functions

A few functions which perform checks on which category a char could fall under: letter, upper case, lower case, number or symbol

Submitted By: gabehabe
Actions:
Rating:
Views: 587

Language: C++

Last Modified: August 6, 2008
Instructions: No need to use all the functions together, they're completely independent of one another. Just copy the one(s) that you need =)

Snippet


  1. /*
  2. * Some simple ASCII functions to check which category a char falls into
  3. * Whether it be a letter (upper or lower case) a number or a symbol
  4. * author: Danny Battison
  5. * contact: gabehabe@hotmail.com
  6. */
  7.  
  8. #include <iostream> // used in the example only
  9.  
  10.  
  11. // some preprocessor if statements to check if
  12. // true and false have already been defined
  13. // (if not, we define them)
  14. #ifndef true
  15. #define true 1
  16. #endif
  17.  
  18. #ifndef false
  19. #define false 0
  20. #endif
  21.  
  22. bool isLower (char ch)
  23. { // check if a character is lower case (a-z)
  24.     if (ch >= 'a' && ch <= 'z')
  25.         return true;
  26.     else return false;
  27. }
  28.  
  29. bool isUpper (char ch)
  30. { // check if a character is upper case (A-Z)
  31.     if (ch >= 'A' && ch <= 'Z')
  32.         return true;
  33.     else return false;
  34. }
  35.  
  36. bool isLetter (char ch)
  37. { // check if a character a character (A-Z || a-z)
  38.     if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= '\z'))
  39.         return true;
  40.     else return false;
  41. }
  42.  
  43. bool isDigit (char ch)
  44. { // check if a character is a number (0-9)
  45.     if (ch >= '0' && ch <= '9')
  46.         return true;
  47.     else return false;
  48. }
  49.  
  50. bool isSymbol (char ch)
  51. { // this one is more broken up.
  52. // if you look at the ascii table, you will see that symbols are scattered
  53. // into several groups, so we perform checks to see if it is between
  54. // the values of said groups
  55.     if ((ch >= '!' && ch <= '/') || (ch >= ':' && ch <= '@')
  56.       ||(ch >= '[' && ch <= '`') || (ch >= '{' && ch <= '~'))
  57.         return true;
  58.     else return false;
  59. }
  60.  
  61. /** EXAMPLE USAGE **/
  62. int main ()
  63. {
  64.     char A = 'A';
  65.     char a = 'a';
  66.     char n = '4';
  67.     char s = ';';
  68.     // remember, by outputting a bool, we will get 0 or 1
  69.     // 0 is false, and 1 is true
  70.     std::cout << isLetter(A) << isUpper(A) << isLower(A) << isDigit(A) << isSymbol(A) << std::endl
  71.               << isLetter(a) << isUpper(a) << isLower(a) << isDigit(a) << isSymbol(a) << std::endl
  72.               << isLetter(n) << isUpper(n) << isLower(n) << isDigit(n) << isSymbol(n) << std::endl
  73.               << isLetter(s) << isUpper(s) << isLower(s) << isDigit(s) << isSymbol(s) << std::endl;
  74.  
  75.     std::cin.get ();
  76.     return EXIT_SUCCESS;
  77.  
  78. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month