Chapter Four - Arrays and String Manipulation
Chapter Four - Arrays and String Manipulation
Computer Programming
3
Array Definition
An array is a series of elements of the same type placed in
contiguous memory locations
Individual elements can be referenced by an index to a
unique identifier.
For example, five values of type int can be declared as an
array without having to declare 5 different variables (each
with its own identifier).
Using an array, the five int values are stored in contiguous
memory locations, and all five can be accessed using the
same identifier, with the proper index.
Like a regular variable, an array must be declared before it
is used.
A typical declaration for an array in C++ is:
int
Regular arrays of local scope are left uninitialized. their contents are
undetermined at the point the array is declared.
But the elements can be explicitly initialized to specific values when it
is declared, by enclosing those initial values in braces {}.
For example:
int age [5] = { 16, 2, 77, 40, 10 }; or int age[ ]={16, 2, 77, 40,
10};
The number of values between braces {} shall not be greater than the
number of elements in the array, If declared with less, the remaining 5
Accessing the value of an Array
The values of any of the elements in an array can be
accessed just like the value of a regular variable of the
same type.
The syntax is:
name[index]
Following the previous examples in which age had 5
elements and each of those elements was of type int, we
can access the value as the following example :-
◦ For example, the following statement stores the value 75
in the third element of age:
age [2] = 75;
◦ and, for example, the following copies the value of the
third element of age to a variable called x:
x = age[2];
6
Multidimensional arrays
8
Arrays as parameters
9
Strings: Definition, accessing Strings
Character sequences
◦ C++ has a powerful string class to handle and
manipulate strings of characters.
◦ However, because strings are, in fact, sequences of
characters, we can represent them also as plain arrays
of elements of a character type.
◦ For example, the following array:
char name [20];
◦ is an array that can store up to 20 elements of type
char.
◦ Therefore, this array has a capacity to store sequences
of up to 20 characters. But this capacity does not need
to be fully exhausted
10
Strings: Definition, accessing Strings
Character sequences
◦ char name [20];
◦ For example, either the sequence "Hello" or the sequence
"Merry Christmas" can be stored in name, since both would fit
in a sequence with a capacity for 20 characters.
◦ By convention, the end of strings represented in character
sequences is signaled by a special character: the null character,
whose literal value can be written as '\0' (backslash, zero).
◦ Because arrays of characters are ordinary arrays, they follow
the same rules as these.
◦ For example, to initialize an array of characters with some
predetermined sequence of characters, we can do it just like
any other array:
char myword[ ] = { 'H', 'e', 'l', 'l', 'o', '\0' };
11
Strings: Definition, accessing Strings
Character sequences
◦But arrays of character elements have another way to be initialized: using
string literals directly.
◦The initialization can be specified by enclosing the text between double quotes
(").
◦For example:
"the result is: "
◦This is a string literal, probably used in some earlier example.
◦Sequences of characters enclosed in double-quotes (") are literal constants.
This means that string literals always have a null character ('\0') automatically
appended at the end.
12
Strings: Definition, accessing Strings
Character sequences
◦In fact, because string literals are regular arrays, they have the
same restrictions as these, and cannot be assigned values.
◦Expressions (once myword has already been declared as above),
such as:
myword = "Bye";
myword[ ] = "Bye";
◦would not be valid, like neither would be:
myword = { 'B', 'y', 'e', '\0' };
◦This is because arrays cannot be assigned values. Note, though,
that each of its elements can be assigned a value individually.
◦For example, this would be correct:
myword[0] = 'B';
myword[1] = 'y';
myword[2] = 'e';
myword[3] = '\0';
13
Strings: Definition, accessing Strings
Character sequences
◦In fact, because string literals are regular arrays, they have the
same restrictions as these, and cannot be assigned values.
◦Expressions (once myword has already been declared as above),
such as:
myword = "Bye";
myword[ ] = "Bye";
◦would not be valid, like neither would be:
myword = { 'B', 'y', 'e', '\0' };
◦This is because arrays cannot be assigned values. Note, though,
that each of its elements can be assigned a value individually.
◦For example, this would be correct:
myword[0] = 'B';
myword[1] = 'y';
myword[2] = 'e';
myword[3] = '\0';
14
Strings: Definition, accessing Strings
In C++, defines a specific type for strings (class string).
String class a compound type variable that store sequences of
characters, such as words or sentences. A very useful feature!
A first difference with fundamental data types is that in order
to declare and use objects (variables) of this type, the
program needs to include the header where the type is
defined within the standard library (header <string>):
Syntax to define string is
string name=“”;
Fro example :
string question2 = "Where do you live? ";
15
End of Chapter Four
Thank You
16