Programming Tutorials

Character Arrays

By: aathishankaran in Java Tutorials on 2007-02-01  

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[1] = 'I';
Char_array[2] = 'N';
Char_array[3] = 'G';
Char_array[4] = '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.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)