www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 16: Character Arrays Bookmark and Share
Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
While dealing with words and sentences, we actually make use of character arrays. Up to now, we were dealing with integer arrays and storing integer values. Here we have to see what needs to be done for storing a name. A simple variable can't be used to store a name (which is a string of characters) as a variable stores only a single character. We need a character array to grab a name. A character array is not different from an integer array. To declare a character array, we will write as under:

char name [100] ;

In this way, we declare a string or character array. There are some special properties of character arrays. Suppose that we declare an array of 100 characters. We enter a name with 15-20 characters. These characters in the array occupy 15-20 character spaces. Now we have to see what has happened to the remaining character spaces in the array. Similarly, a question arises, will an array, displayed on the screen, show 100 characters with a name in 15-20 spaces and blanks for the remaining. Here C has a character handling capability i.e. the notion of strings. When we place a string in a character array, the computer keeps a mark to identify that the array was of this size while the string stored in it is of the other size. That marker is a special character, called null character. The ASCII code of null character is all zeros. In C language, we represent the null character as “\0”. C uses this character to terminate a string. All strings are terminated with the null character.

We can store a string in a character array named name simply by using the cin statement in the following way:

cin >> name ;

In the above statement, there is an array on right hand side of cin instead of a simple variable. The cin stream has a built-in intelligence that allows the compiler (program) to read whole string at a time rather than a single character as in case of simple variable of type char. The compiler determines that the name is not a simple variable. Rather it is a string or character array. Thus cin reads a character array until the user presses the enter key. When enter key is pressed, cin takes the whole input (i.e. string) and stores it into the array name. The C language, by itself, attaches a null character at the end of the string. In this way, the total number of spaces occupied in the array by the string is the number of characters entered by the user plus 1 (this one character is the null character inserted at the end of the string by C automatically). The null character is used to determine where the populated area of the array has ended. If we put a string larger than the size of the array in absence of a null character in it, then it is not possible to determine where a string is terminated in the memory. This can cause severe logical error. So, one should be careful while declaring a character array. The size of array should be one more than the number of characters you want to store.

Initialization Of Character Arrays

We can initialize a character array by giving a list of characters of the string.We write the characters of this string one by one in single quotes (as we write a single character in single quotes), separated by commas and enclosed in curly braces. So the initialization line will be as under

char name [100] = {‘i’, ‘m’, ‘r’, ‘a’, ‘n’};

we can also write the string on right hand side in double quotes as:

char name [100] = “imran” ;

The easy way to initialize a character array is to assign it a string in double quotes. We can skip the size of the array in the square brackets. We know that the compiler allocates the memory at the declaration time, which is used during the execution of the program. In this case, the compiler will allocate the memory to the array of size equal to the number of characters in the provided string plus 1 (1 is for the null character that is inserted at the end of string). Thus it is better to initialize an array in the following way.

char name [] = “Hello World” ;

In the above statement, a memory of 12 characters will be allocated to the array name as there are 11 characters in double quotes (space character after Hello is also considered and counted) while the twelfth is the null character inserted automatically at the end of the string.

We can do many interesting things with arrays. Let’s start with reading a string (for example your name) from keyboard and displaying it on the screen. For this purpose, we can write the following code segment

char name [100] ;
cout << “Please enter your name : “ ;
cin >> name ;

In the cin statement, when the user presses the enter key the previous characters entered, that is a string will be stored in the array name. Now we have a string in the array name. We can display it with cout statement. To display the string, we have stored in name. We can write as under:

cout << name ;

This will display the string. Alternatively, we can use a loop to display the string. As the string is an array of characters, we can display these characters one by one in a 'for loop'. We can write a loop as under

for ( i = 0 ; i < 100 ; i ++ )
cout << name [ i ] ;

Thus this loop will display the characters in the array one by one in each iteration. First, it will display the character at name [0], followed by that at name [1] and so on. Here we know that the string in the array is terminated by a null character and after this null character, there are random values that may not be characters (some garbage data) in the array. We don’t want to display the garbage data that is in the array after this null character. While using the statement cout << name; the cout stream takes the characters of the array name up to the null character and the remaining part of the array is ignored. When we are displaying the characters one by one, it is necessary to stop the displaying process at the end of a string (which means when null character is reached). For this purpose, we may put a condition in the loop to terminate the loop when the null character is reached. So we can use if statement in the loop to check the null character. We can modify the above for loop so that it could terminate when null character reaches in the array.

for ( i = 0 ; i < 100 ; i ++ )
{ if (name [ i ] == ‘\0’)
break ;
cout << name [ i ] ;
}



NEXT>>>>>Lesson 17. Functions and Arrays



Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.