C Tutorial - Chapter 13

CHARACTER AND BIT MANIPULATION

UPPER AND LOWER CASE

Example program ------> UPLOW.C

Examine the program named UPLOW.C for an example of a program that does lots of character manipulation. More specifically, it changes the case of alphabetic characters. It illustrates the use of four functions that have to do with case. It should be no problem for you to study this program on your own and understand how it works. The four functions on display in this program are all within the user written function, mix_up_the_chars(). Compile and run the program with the file of your choice. The four functions are;

     isupper(c);    Is the character upper case?
     islower(c);    Is the character lower case?
     toupper(c);    Make the character upper case.
     tolower(c);    Make the character lower case. 

Many more classification and conversion routines are listed in the reference material for your compiler. You should spend time studying these at this time to get an idea of what functions are available.

CLASSIFICATION OF CHARACTERS

Example program ------> CHARCLAS.C

Load and display the next program, CHARCLAS.C for an example of character counting. We have repeatedly used the backslash n character representing a new line. These are called escape sequences, and some of the more commonly used are defined in the following table;

     \n    Newline 
     \t    Tab 
     \"    Double quote 
     \\    Backslash
     \0    NULL (zero) 

Consult your compiler documentation for a complete list of escape sequences available with your compiler.

By preceding each of the above characters with the backslash character, the character can be included in a line of text for display, or printing. In the same way that it is perfectly all right to use the letter n in a line of text as a part of someone's name, and as an end-of-line, the other characters can be used as parts of text or for their particular functions.

This example program uses three of the functions that can determine the class of a character, and counts the characters in each class. The number of each class is displayed along with the line itself. The three functions are as follows;

    isalpha(c); Is the character alphabetic?
    isdigit(c); Is the character a numeral?
    isspace(c); Is the character any of, \n, \t, or blank? 

As noted above, many more classification routines are available with your compiler.

This program should be simple for you to find your way through, so no explanation will be given. It was necessary to give an example with these functions used. Compile and run this program with any file you choose.

THE LOGICAL FUNCTIONS

Example program ------> BITOPS.C

Load and display the program BITOPS.C. The functions in this group of functions are used to do bitwise operations, meaning that the operations are performed on the bits as though they were individual bits. No carry from bit to bit is performed as would be done with a binary addition. Even though the operations are performed on a single bit basis, an entire byte or integer variable can be operated on in one instruction. The operators and the operations they perform are given in the following table;

   &   Logical AND, if both bits are 1, the result is 1. 
   |   Logical OR, if either bit is one, the result is 1. 
   ^   Logical XOR, (exclusive OR), if one and only one 
                             bit is 1, the result is 1. 
   ~   Logical invert, if bit is 1, the result is 0, and 
                            if bit is 0, the result is 1.
 

The example program uses several fields that are combined in each of the ways given above. The data is in hexadecimal format. It will be assumed that you already know hexadecimal format if you need to use these operations. If you don't, you will need to study it on your own. Teaching the hexadecimal format of numbers is beyond the scope of this tutorial. Be sure to compile and execute this program and observe the output.

THE SHIFT INSTRUCTIONS

Example program ------> SHIFTER.C

The last two operations to be covered in this chapter are the left shift and the right shift instructions. Load the example program SHIFTER.C for an example using these two instructions. The two operations use the following operators;

    <<    n Left shift n places.
    >>    n Right shift n places. 

Once again the operations are carried out and displayed using the hexadecimal format. The program should be simple for you to understand on your own, there is no tricky code.

WHERE DO I GO FROM HERE?

Now that you have completed this tutorial, you are filled with knowledge of the C programming language, but you have relatively little experience with using it.

The most important recommendation I can make is to write programs in C. Writing C code, finding and fixing the errors that you will inadvertently introduce into the code, and finally seeing your program execute just the way you intended it to, provides a great feeling of accomplishment.

The more you expose yourself to the C programming language, the more you will learn about it, and the more you will enjoy using it.

Good luck!

Return to Table of Contents


Copyright © 1998 David Alan Quick - Last update, 15 April 1998
David Alan Quick - sundog97@hotmail.com -
Please email any comments or suggestions.