4.3 Characters

[ Table of Contents ] Prev ] Chapter Overview ] Next ] [ Glossary/Index ]

The built-in type Character is an enumeration type with 256 values known as the Latin-1 character set (declared in package Standard). The first 128 of these values are the ASCII character set. The digits '0' to '9' occupy position numbers 48 through 57. The upper case letters 'A' to 'Z' occupy position numbers 65 through 90. The lower case letters 'a' to 'z' occupy position numbers 97 through 122. The other values are punctuation marks, control characters and graphic characters. A single character enclosed by apostrophe marks, such as 'A', is known as a character literal.

The built-in type Wide_Character is an enumeration type with 65,536 values known as the Unicode set. It includes characters used in many alphabets around the globe and other characters such as mathematical symbols.

Example Programs Illustrating the Use of Characters and Attributes  

Several programs involving Character objects have already been presented in earlier chapters. For example, in the first package example of Chapter 2 we used a Get procedure to obtain either a 'Y' or a 'N' to determine which path to take, and in the ADO and ADT stack package examples, characters were pushed and popped.

Here is an example program that includes the use of the Val, Pos, Succ and Pred attributes.

Im4-4.gif (2021 bytes)

This program is simply a main procedure that depends upon the Ada.Text_IO package.

 

Note the repeated use of the "for loop" version of a loop statement, as described in Chapter 3.

Source Code Listing

----------------------------------------------------------
-------------------- Show_Characters ---------------------
--  This procedure displays sets of characters or their 
--  positions, using Val, Pos, Succ and Pred attributes. 
----------------------------------------------------------
with Ada.Text_IO;
use  Ada.Text_IO;
procedure Show_Characters is
  Ch : Character := 'A';
begin
  
  for I in 65..74 loop
    Put("  ");  
    Put(Character'Val(I));                 -- Val  
  end loop;
  
  New_Line;
  for I in 1..10 loop
    Put(Integer'Image(Character'Pos(Ch))); -- Pos and Image
    Ch := Character'Succ(Ch);              -- Succ
  end loop;
  
  New_Line;
  New_Line;
  for I in 1..10 loop
    Put("  ");
    Ch := Character'Pred(Ch);              -- Pred
    Put(Ch);  
  end loop;
  
  New_Line;
  for I in 1..10 loop  
    Put("  ");
    Put(Ch);
    Ch := Character'Succ(Ch);              -- Succ
  end loop;
    
end Show_Characters;
----------------------------------------------------------

The above program produces the following output:

       A  B  C  D  E  F  G  H  I  J
      65 66 67 68 69 70 71 72 73 74

       J  I  H  G  F  E  D  C  B  A
       A  B  C  D  E  F  G  H  I  J

Related Topics

3.6 loop and exit Statements 4.1 Type System Overview

[ Back to top of pagePrev ] Next ]