Char Array
Char Array
Lab 14
Topic Basics of Charter array
Objective Learning objectives of this lab are to get hands on experience on the concepts of character array.
Character array:
An array is a collection of a fixed number of components (also called elements) all of the same data type and in contiguous
(that is, adjacent) memory space. An array whose components are of type char.
char list[10];
Above statement is used for creating an array. But as we discussed above when a memory location is reserved for this
array it holds some values which are not assigned by user. So those values are considered as garbage value. In order to
avoid garbage value, it is a good practice to assign values at the time of creation of array.
Assigning value at the time of declaring of array or variable is called initialization statement. There are multiple ways of
initialize an array.
char list[5]={‘a’,’b’,’c’,’d’,’e’};
char list[5]={‘a’,’b’};
on remaining indexes null will be assigned as initial value in case of partial array initialization.
Array input:
We can take input from user into an array. We can take input index wise one by one and we can also take input at specific
index number.
cin>> list[0]; //taking input on index 0 which is the first element of an array.
Input in whole array:
We use repetition statement(loops) for taking input in whole array. We can take input one by one as mention above but it
is not considered as a good practice.
for(int i=0;i<size;i++)// i is used as index number and size is the number element you want to enter.
{
cin>>list[i]; // taking input at specific index i. After every iteration value of i will be updated.
}
Array output:
We can display the values of array on console as output to user. We can display index wise one by one and we can also
take input at specific index number.
cout<< list[0]; //display the value of index 0 which is the first element of an array.
We use repetition statement(loops) for display whole array. We can display elements one by one as mention above but it is
not considered as a good practice.
for(int i=0;i<size;i++)// i is used as index number and size is the number element you want to display.
{
cout<<list[i]; // display specific index i. After every iteration value of i will be updated.
}
Array index out of bound:
Index of an array is in bounds if index is between 0 and ARRAY_ SIZE - 1, that is, 0 <= index <= ARRAY_SIZE - 1. If index is
negative or index is greater than ARRAY_SIZE - 1, then we say that the index is out of bounds.
C-String:
A character array which is terminated at null is called C-String. C-String is quite simmilar with character array but, some of
the working is a bit different. We can declare a C-String same as we declare a character array. But initialization is a bit
different.
char list[5]={“abcd”};
In case of character array we write character in single qoute which are comma seprated in case of character array. In case
of C-String we write a string in double qoutes. One more differecne is we need an extra index for storing null in case of c-
string. Because a null character will be placed at the end of the c-string.
Acessing a c-string:
We can access the element of a c-string same as we did in case of character array. But input and output of a c-string is a bit
different than a character array.
Input a c-string:
We can take the input into c-string same as we discuss in character array. But there is an issue if we need to read a full
name using this method it falis. Because, extraction operator, >>, skips all leading whitespace characters and stops reading
data into the current variable as soon as it finds the first whitespace character. So we need something else in order to take
complete input (with whitespaces) into a c-string.
Using this method, we can take whitespaces as input from user. This method helps us to take input as character by
character and as a whole string as well.
Output of c-string:
There is a limitation with insertion operator, <<, it displays the data till null character. So if we are displays character array
with this method it may shows some garbage values after the data.
Task 1
Write a program that prints character array using loop with the help of size and Null character ‘\0’.
Task 2
Write a program that prints character array in reverse order.
Task 3
Write a program that counts the length of character array.
Task 4
Write a program that prints only even index values.
Task 5
Write a function which prints vowels character present in the character array entered by the user.
Task 6
Write a function which returns the number of vowels present in the character array entered by the user.
Task 7
Write a function which returns the number of capital, small letter present in the character array entered by the user.
Task 11
Write a function based program in which two character arrays are accepted from the user. Your program then merges the
two arrays in a third array but the entries are done in alphabetical order.
Example:
Enter 1st array:
D R P I E A N
Output:
A B D E G I K L M N O P Q R
Task 12
Declare and initialize an array of size 20 with the characters as shown below.
full_name[20] a B v %/ 1 s h ^ 5 3 # h s Y % @ 2 8 \0
Write a program that to separate integers, alphabets (small and capital), and special characters in
separate arrays.
Output:
integers[] 1 5 3 2 8 \0
alphabets[] a B v s h h s Y \0
Special_characters[] % / ^ # % @ \0