0% found this document useful (0 votes)
182 views

Chapter Four - Arrays and String Manipulation

This document discusses arrays and strings in C++. It begins with an introduction to arrays, including how to declare and initialize one-dimensional and multi-dimensional arrays. It also covers how to access array elements and pass arrays as function parameters. The document then discusses strings, explaining that strings can be represented as character arrays and initialized using string literals. It provides examples of declaring and initializing character arrays to represent strings, and accessing individual string elements. The document concludes with noting the C++ string class as an alternative to character arrays for representing strings.

Uploaded by

Yab Yab
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
182 views

Chapter Four - Arrays and String Manipulation

This document discusses arrays and strings in C++. It begins with an introduction to arrays, including how to declare and initialize one-dimensional and multi-dimensional arrays. It also covers how to access array elements and pass arrays as function parameters. The document then discusses strings, explaining that strings can be represented as character arrays and initialized using string literals. It provides examples of declaring and initializing character arrays to represent strings, and accessing individual string elements. The document concludes with noting the C++ string class as an alternative to character arrays for representing strings.

Uploaded by

Yab Yab
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CoSc 131

Computer Programming

Instructor : Welde Janfa


Chapter 4

Arrays and String


Manipulation
OUTLINE
1. Array Definition
2. Array referencing
3. One dimensional and multidimensional
arrays
4. Strings: Definition, accessing Strings

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:

type name [elements];


4
Array Declaration and Initialization
 Syntax :-
type name [elements];
 For example :-
int age [5];
0 1 2 3 4
age

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

 Multidimensional arrays can be described as "arrays of


arrays".
 For example, a bi-dimensional array can be imagined as a
two-dimensional table made of elements, all of them of a
same uniform data type
1 2 3 4
1
2
3
 A bi-dimensional array of 3 per 5 elements of type int.
 The C++ syntax for this is:

type name [elements ][elements];


 For example

◦ int period [4][5]


7
Multidimensional arrays

 For example, the way to reference the second element


vertically and fourth horizontally in an expression would
be:
◦ Period [1][2]
 Note:-

(remember that array indices always begin with zero).


 Multidimensional arrays are not limited to two indices (i.e.,
two dimensions).
 They can contain as many indices as needed.
 . For example:

char century [100][365][24][60][60];

8
Arrays as parameters

 At some point, we may need to pass an array to a function as


a parameter.
 In C++, it is not possible to pass the entire block of memory
represented by an array to a function directly as an
argument. But what can be passed instead is its address. In
practice, this has almost the same effect, and it is a much
faster and more efficient operation.
 To accept an array as parameter for a function, the
parameters can be declared as the array type, but with empty
brackets, omitting the actual size of the array. For example:
void procedure (int arg[])
This function accepts a parameter of type "array of int"
called arg.

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.

◦Therefore, the array of char elements called myword can be initialized as


char myword[ ] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char myword[ ] = "Hello";
◦In both cases, the array of characters myword is declared with a size of 6
elements of type char: the 5 characters that compose the word "Hello", plus a
final null character ('\0'), which specifies the end of the sequence

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy