home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTUTOR1.ZIP / CHAP13.TXT < prev    next >
Encoding:
Text File  |  1986-06-30  |  5.3 KB  |  125 lines

  1.                  Chapter 13 - Character and Bit Manipulation
  2.  
  3.  
  4.                             UPPER AND LOWER CASE
  5.  
  6.              Load  and display the program UPLOW.C for an example of 
  7.  
  8.         a  program that does lots of character  manipulation.   More 
  9.  
  10.         specifically,  it changes the case of alphabetic  characters 
  11.  
  12.         around.   It illustrates the use of four functions that have 
  13.  
  14.         to  do with case.   It should be no problem for you to study 
  15.  
  16.         this program on your own and understand how it  works.   The 
  17.  
  18.         four functions on display in this program are all within the 
  19.  
  20.         user written function,  "mix_up_the_line".   Compile and run 
  21.  
  22.         the  program  with  the  file  of  your  choice.   The  four 
  23.  
  24.         functions are;
  25.  
  26.              isupper();     Is the character upper case?
  27.              islower();     Is the character lower case?
  28.              toupper();     Make the character upper case.
  29.              tolower();     Make the character lower case.
  30.  
  31.                         CLASSIFICATION OF CHARACTERS
  32.  
  33.              Load  and display the next program,  CHARCLAS.C for  an 
  34.  
  35.         example of character counting.   We have repeatedly used the 
  36.  
  37.         backslash  n character representing a new line.   There  are 
  38.  
  39.         several  others that are commonly used,  so they are defined 
  40.  
  41.         in the following table;
  42.  
  43.              \n             Newline
  44.              \t             Tab
  45.              \b             Backspace
  46.              \"             Double quote
  47.              \\             Backslash
  48.              \0             NULL (zero)
  49.  
  50.              By  preceding  each of the above  characters  with  the 
  51.  
  52.         backslash character, the character can be included in a line 
  53.  
  54.         of text for display,  or printing.   In the same way that it 
  55.  
  56.         is  perfectly  all right to use the letter "n" in a line  of 
  57.  
  58.         text as a part of someone's name, and as an end-of-line, the 
  59.  
  60.         other  characters can be used as parts of text or for  their 
  61.  
  62.         particular functions. 
  63.  
  64.              The program on your screen uses the functions that  can 
  65.  
  66.         determine   the  class  of  a  character,   and  counts  the 
  67.  
  68.         characters  in  each class.   The number of  each  class  is 
  69.  
  70.         displayed  along with the line itself.   The three functions 
  71.  
  72.         are as follows;
  73.  
  74.              isalpha();     Is the character alphabetic?
  75.              isdigit();     Is the character a numeral?
  76.              isspace();     Is the character any of, \n, \t, 
  77.                               or blank?
  78.  
  79.  
  80.  
  81.                                    Page 92
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.                  Chapter 13 - Character and Bit Manipulation
  92.  
  93.  
  94.  
  95.              This program should be simple for you to find your  way 
  96.  
  97.         through  so no explanation will be given.   It was necessary 
  98.  
  99.         to give an example with these functions used.   Compile  and 
  100.  
  101.         run this program with any file you choose.
  102.  
  103.                            THE LOGICAL FUNCTIONS
  104.  
  105.              Load and display the program BITOPS.C. The functions in 
  106.  
  107.         this  group of functions are used to do bitwise  operations, 
  108.  
  109.         meaning  that  the operations are performed on the  bits  as 
  110.  
  111.         though they were individual bits.   No carry from bit to bit 
  112.  
  113.         is performed as would be done with a binary addition.   Even 
  114.  
  115.         though  the operations are performed on a single bit  basis, 
  116.  
  117.         an entire byte or integer variable can be operated on in one 
  118.  
  119.         instruction.   The operators and the operations they perform 
  120.  
  121.         are given in the following table;
  122.  
  123.              &    Logical AND, if both bits are 1, the result is 1.
  124.              |    Logical OR, if either bit is one, the result is 1.
  125.              ^    Logical XOR,  (exclusive OR),  if one and only one 
  126.  
  127.                     bit is 1, the result is 1.
  128.              ~    Logical invert,  if the bit is 1, the result is 0, 
  129.  
  130.                     and if the bit is 0, the result is 1.
  131.  
  132.              The  example  program  uses  several  fields  that  are 
  133.  
  134.         combined  in each of the ways given above.   The data is  in 
  135.  
  136.         hexadecimal  format.   It  will be assumed that you  already 
  137.  
  138.         know hexadecimal format if you need to use these operations.  
  139.  
  140.         If  you  don't,  you  will need to study  it  on  your  own.  
  141.  
  142.         Teaching  the  hexadecimal format of numbers is  beyond  the 
  143.  
  144.         scope of this tutorial.
  145.  
  146.              Run the program and observe the output.
  147.  
  148.                            THE SHIFT INSTRUCTIONS
  149.  
  150.              The  last two operations to be covered in this  chapter 
  151.  
  152.         are  the left shift and the right shift instructions.   Load 
  153.  
  154.         the example program SHIFTER.C for an example using these two 
  155.  
  156.         instructions.    The   two  operations  use  the   following 
  157.  
  158.         operators;
  159.  
  160.              << n      Left shift n places.
  161.              >> n      Right shift n places.
  162.  
  163.              Once again the operations are carried out and displayed 
  164.  
  165.         using the hexadecimal format.   The program should be simple 
  166.  
  167.         for you to understand on your own, there is no tricky code.
  168.  
  169.  
  170.  
  171.  
  172.                                    Page 93
  173.  
  174.