Character Arrays

By: aathishankaran Viewed: 153135 times  Printer Friendly Format    


Character Arrays

 

Arrays are not confined to numeric elements only. They also can consist of characters. A string is stored as an array of characters.

 

To assign values to character array, the following is written:

 

            Char_array[0] = ‘B’;

            Char_array[0] = ‘I’;

            Char_array[0] = ‘N’;

            Char_array[0] = ‘G’;

            Char_array[0] = ‘O’;

 

It can also be done as:

 

Char_array = “BINGO”;

 

Since a string can be of any length, the end of the string is marked with the character ‘\0’, known as a string terminator

 

B          I             N             G             O            \0

Subscripts->                      0         1               2          3               4          5

 

A string terminator is automatically appended when a character array is initialized with asset of characters (Char_array = “BINGO”} and whenever a string is accepted using the keyword accept.

 

While declaring a character array, the array must be large enough to hold the longest string that is to be read, plus one extra element to hold the string terminator.

 

Initializing Character Arrays

 

char City[11] = “CALIFORNIA”;

 

Example : Finding the length of a character array.

 

PROGRAM_LEN

{

            char String[81]

            integer Length;

            display “Please Enter a String:”;

            accept String;

            Length = 0;

            While ( String [Length] !=’\0’)

{

            Length=Length+1;

}

display ”Length of the String is”;

display Length

}

 

Scanning character by character in an array is also termed as traversing.

 

 

 



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial