Characters, Strings, and I/O
Since the beginning of this course and up to this point, our processing has usually involved numbers. The following questions reflect many of the common processing tasks we have encountered.
- What frequencies should be used when a Scribbler 2 robot beeps, and how long should the beeps last?
- If a robot moves forward or backward or if the robot turns, how fast should it move and for how long?
In this module, we consider expand our opportunities for processing by exploring text: uppercase letters, lowercase letters, punctuation, and other characters we can type on most keyboards.
Conceptually, working with characters follows a reasonably simple idea. Behind the scenes, we know that all data within a computer are stored using circuitry, and our study of numbers indicates that data storage is based on individual circuits being on or off (often represented as 1 or 0). With binary sequences the foundation of digital computers, we can store characters (e.g., letters, punctuation, etc.), as long as we can agree upon what coding (sequence of 0's and 1's) should represent each character.
-
Character sets: Within C, a mapping of characters to binary codes is called a character set or charset.
-
Strings: With a character set established, then a sequence of characters (e.g., the sentences in this paragraph) can be stored as an array.
-
Input: Since characters often correspond to what users type at a keyboard, programs can utilize user input, provided the programming language has functions to retrieve and process this character input.
-
Text processing: Once text has been typed and retrieved by a program, additional functions may be helpful in editing or otherwise processing the text.
-
Output: After processing text, further functions and procedures are needed to print output on a monitor or printer.
This module describes both the data types available within C for storing character data and some of the standard C libraries for handling textual input, processing, and output.
As we shall observe, some elements of working with character data may be considered reasonably straight forward. However, subtleties can arise throught data entry, processing, and printing, and we will need to be careful as we work with details.