0% found this document useful (0 votes)
21 views5 pages

Char Array

Uploaded by

umerfaisal678
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Char Array

Uploaded by

umerfaisal678
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction to Computing

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.

Declaration statement of an array:

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.

Initialization statement of an array:

Assigning value at the time of declaring of array or variable is called initialization statement. There are multiple ways of
initialize an array.

Initialize an array with null character:

char list[5]={}; OR char arr[5]{};

Full array initialization with different elements:

char list[5]={‘a’,’b’,’c’,’d’,’e’};

Partial array initialization with different elements:

char list[5]={‘a’,’b’};

on remaining indexes null will be assigned as initial value in case of partial array initialization.

Accessing array components:

Generic way of accessing an array is: IdentifierName[index#];

char list[10]; //list[0] is use for accessing first element of an array.

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.

Input at specific index:

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.

Output of specific index:

cout<< list[0]; //display the value of index 0 which is the first element of an array.

Output of whole 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.

Initialization statement of a C-String:

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.

We can solve this issue using cin.get() method.


cin.get():

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.

Taking input character by character:

cin.get(ch); // ch is a character variable.

Taking input as a string:

cin.get(list,10) //list is a c-string and 10 is representing the input buffer size.

Output of c-string:

we can display a c-string using insertion operator, <<.

cout<<list; //list is a c-string.

What if we display a character array like above mention method?

cout<<arr; //arr is a character array.

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

Enter 2nd array:


B G K Q M O L

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

You might also like