CHARACTER ARRAY
The procedure for declaring character array is almost the same as for other data types such as integer or floating point. One
can declare the character array by means of alphanumeric characters.
The general format
of the character array is,
storage_class character_data_type array_name
[expression]
The storage-class is optional and it may be
either one of the scope of the variable such as automatic, external, static, or
register; the name can be any valid C++ identifier and the expression a
positive integer constant.
For example,
char page [40]
char sentence [300];
static char line [50];
The basic structure of the character
array is
R
|
A
|
V
|
I
|
C
|
\0
|
Each
element of the array is placed in a definite memory space and each element can
be accessed separately. The array element should end with the null character as a reference for the
termination of a character array.
Initializing the character array Like a integer and floating point array, the
character array also be initialized.
For example,
char colour [3] = “RED”;
The elements would
be assigned to each of the character array position in the following way:
colour [0] = ‘R’;
colour [1] = ‘e’;
colour [2] = ‘D’;
The character array always terminate with the null character, that is, a back-slash
followed by a letter zero (not letter ‘o’ or ‘O’), and the computer will treat
them as a single character.
The null character will be added automatically by
the C++ compiler provided there is enough space to accommodate the character.
For example,
char name [5] = “ravic” // error
The following
assignment would be for the each cell,
name [0] = ‘r’;
name [1] = ‘a’;
name [2] = ‘v’;
name [3] = ‘i’;
name [4] = ‘c’;
The above
declaration is wrong because there is no space to keep the null character in
the array as a termination character and it can be corrected by redefining the
above array as
char name [6] = “ravic” // right
The following
assignment would be for the each cell,
name [0] = ‘r’;
name [1] = ‘a’;
name [2] = ‘v’;
name [3] = ‘i’;
name [4] = ‘c’;
name [5] = ‘\0’;
Following is a valid array
declaration:
char line[] = “this is a test program”
The square
brackets can be empty, as the array size would have been specified as part of
the array definition.
PROGRAM
A program to
initialize a set of characters in an one dimensional character array and to
display the contents of the given array.
// example 5.10
#include <iostream.h>
void main (void)
{
int i;
static char name [5] = {
‘r’,’a’,’v’,’i’,’c’};
cout << “Contents of the array”
<< endl;
for (i = 0; i <= 4; ++i) [
cout << “name[ “ << i
<< “ ] = “ name [i] << endl;
}
}
Output of the above
program
Contents
of the array
name [ 0 ] = r
name [ 1 ] = a
name [ 2 ] = v
name [ 3 ] = i
name [ 4 ] = c
PROGRAM
A program to initialize a set of character strings in an one
dimensional character array and to display the contents of the array.
// example 5.12
#include <iostream.h>
void main (void)
{
int i;
static char name [] = “this is a test
program”;
cout << “Contents of the array”
<< endl;
for (i = 0; name [i] != ‘\0’; ++i) {
cout << “name[ “ <<
i<< “] =” << name[i] <<endl;
}
}
Output of the above
program
Contents of the array
name [ 0] = t
name [ 1] = h
name [ 2] = i
name [ 3] = s
name [ 4] =
name [ 5] = i
name [ 6] = s
name [ 7] =
name [ 8] = a
name [ 9] =
name [ 10] = t
name [ 11] = e
name [ 12] = s
name [ 13] = t
name [ 14] =
name [ 15] = p
name [ 16] = r
name [ 17] = o
name [ 18] = g
name [ 19] = r
name [ 20] = a
name [ 21] = m
PROGRAM
A program to read a line from the keybord and to display the
contents of the array on the screen.
// example 5.13
// reading a line
#define MAX 80
#include <iostream.h>
void main (void)
{
char line [MAX];
cout << “Enter a line of text \n”;
cin.get (line, MAX, ‘\n’); //reading a line
cout << “output from the array”
<< endl;
cout << line;
Output of the above
program
Enter a line of text
this is a test program
output from the array
this is a test program
PROGRAM
A program to read a set of lines from the keyboard and to
store it in an one dimensional array and to display the contents of the array
on the screen.
// example
5.14
// reading a set of lines
#define MAX 80
#include <iostream.h>
void main (void)
{
char line [MAX]
cout << “Enter a set of lines
and terminate with @ \n”;
cin.get (line,MAX, ’@’); reading a string
cout << “output from the array”
<< endl;
cout << line;
}
Output of the above
program
Enter a set of lines and terminate with @
This is a
test program
by ravic
@
Output from the array
This is a
test program
by ravic
PROGRAM
A program to read a set of lines from the keyboard; store it
in an one dimensional array;
Display the contents of the array and the number of the
characters on the screen.
//finding the number of characters
#define MAX 200
#include <iostream.h>
void main (void)
{
char line [MAX];
int nch;
char ch;
int number (char line [] ) ;
cout << “Enter a set of lines and
terminate with @”;
cout << endl;
cin.get (line,MAX,’@’);
nch = number (line)-1;
cout << output from the array”
<< endl;
cout << line << endl;
cout << endl;
cout << “Number of characters =”
<< nch << endl;
}
int number (char a [])
// function to find the number of
characters
{
int i;
i = 0;
while (a [i] != ‘\0’)
++i;
return (i);
}
Output of the above
program
Enter a set of lines and terminate with @
this is
a test
@
output from the array
this is
a test
Number of characters =15
PROGRAM 5.16
A program to read a set of lines from the keyboard; store it
in an one dimensional array A; copy the
contents of A to an array B and display the contents of arrays A and B
separately.
For example,
t
|
h
|
i
|
s
|
..
|
|
A
t
|
h
|
i
|
s
|
..
|
|
B
//string copy
#define MAX 200
#include <iostream.h>
void main (void)
{
char a[MAX], b[MAX];
void stringcopy (char b[], char a[]);
cout << “Enter a set of lines and
terminate with @”;
cout << endl;
cin.get (a,MAX,’@’);
stringcopy (b,a);
cout << “output from the A array”
<< endl;
cout << b << endl;
cout << “output from the B array”
<< endl;
cout << b << endl;
}
void stringcopy (char b [], char a[])
//copy the contents of A to B
{
int i;
i = 0;
while ( a[i] != ‘\0’) {
b[i] = a[i];
i++;
}
b[i++] = ‘\0’;
}
Output of the above
program
Enter a set of lines and terminate with @
this is
by ravic
@
Output from the A array
this is
a test program
by ravic
output from the B array
this
is
a test program
by ravic
PROGRAM
A program to read a set of lines from the keyboard; store in
the array A; again read a set of lines from the keyboard and store it in the
array B; copy the contents of array A and array B into an array TOTAL and
display the contents of arrays A and B and TOTAL separately.
For example,
t
|
h
|
i
|
s
|
..
|
|
A
r
|
a
|
..
|
|
|
|
B
t
|
h
|
i
|
s
|
r
|
a
|
..
|
|
TOTAL
//string concatenate
#define MAX 200
#include <iostream.h>
void main (void)
{
char a[MAX], b[MAX];
void stringconcat (char total [], char
b[]);
cout << “Enter a set of lines and
terminate with @”;
cout << endl;
cin.get (a,MAX,’@’)
cout << “Enter again a set of
lines and terminate with $“;
cout << endl;
cin.get (b,MAX,’$’);
stringconcat (total,a,b);
cout << endl;
cout << “output from the A array”
<< endl;
cout << a << endl;
cout << ”output from the B array”
<< endl;
cout << b << endl;
cout << “output from the TOTAL
array” <<endl;
cout << total << endl;
}
void stringconcat (char total[], char source1
[],
char source2 []
// copy the contents of source1 and source2
to the total
{
int i,j;
i =0;
while ( source1 [i] != ‘\0’) {
total[i] = source1[i];
i++;
j++;
}
total [i++] = ‘\0’;
}
Output of the above
program
Enter a set of lines and terminate with @
this is
a test program
@
Enter again a set of lines and terminate
with $
by ravic
of iti palghat
$
Output from the A array
this is
a test program
output from the B array
by ravic
of iti palghat
output from the TOTAL array
a test program
by ravic
of iti palghat