The basic element of text is represented as type Character. A variable of type character can hold, well, a character. More precisely, something of type Character can represent any one of the 256 possible characters in the ``Latin-1'' set. Type Character is sufficient for handling most languages written using Latin-based characters, such as English, French, and Spanish. Latin-1 is a superset of the ASCII character set (also called ISO 646), so Character is the right type for processing ASCII text files.
Ada 95 also defines a type called Wide_Character. If you need to handle non-Latin alphabets (such as Chinese or Arabic) you would use Wide_Character instead of Character. A Wide_Character can represent any character from the entire ISO 10646 character set. We won't discuss Wide_Character right now, but it's important to know that it's available if you need it.
Constants of type Character are written between single quotes (this is the same way it's done in C and C++). Thus, 'a' is the constant ``lower-case A'', and ''' is the constant ``single quote mark.''
Ada 95 defines a large set of predefined operations to help manipulate characters. Many of them are defined in the package Characters.Handling; if you're curious you can look at the complete declaration of package Characters.Handling in the RM section A.3.2. For example, Characters.Handling defines a function named To_Lower that accepts a single character and returns a lower case version of that character (if there is one - otherwise it just returns the character it was given).
Package Text_IO has a `Get' operation that can read a single character, and a `Put' operation that can write a single character.
Let's put these ideas together into a simple program that asks a yes-or-no question, gets a character as a response, and does something based on the user response:
with Text_IO; use Text_IO; with Ada.Characters.Handling; use Ada.Characters.Handling; procedure Yes_No is Response : Character; begin Put("Would you like me to say Hello?"); Get(Response); -- Get first character. if (To_Lower(Response) = 'y') then Put("Hello!"); else Put("Okay, I won't."); end if; end Yes_No;
Ada's type Character is similar to C's ``char'' type, and package Characters.Handling is the Ada 95 equivalent of C's ctypes.h file.
Ada permits compilers to support additional ``local'' character sets as a compile-time option, but Ada compilers must support at least Latin-1 and ISO 10646.
Which of the following is a Character constant?
|Go back to the previous section||Skip to the next section||Go up to lesson 8 outline|
David A. Wheeler (email@example.com)
The master copy of this file is at
The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s8s1.htm".