Professional Documents
Culture Documents
Strings
Module outline
2
Single-Dimensional Arrays
3
Array Literals
▪ The array cars has four elements and since we assigned these
elements to it upon declaration it is an array literal.
▪ To create an array of three integers, you could write:
4
Access the Elements of an Array
▪ Note: Array indexes start with 0: [0] is the first element. [1] is the
second element, etc.
5
Change an Array Element
6
Arrays and Loops
▪ You can loop through the array elements with the for loop.
▪ The following example outputs all elements in the cars array:
▪ This example outputs the index of each element together with its
value:
7
Activity 1 (10 mins)
8
The foreach Loop
9
Activity 2
▪ Use a for each loop to display the numbers in the previous Activity.
▪ Solution:
10
Omit Array size
▪ In C++, you don't have to specify the size of the array. The compiler is smart
enough to determine the size of the array based on the number of inserted
values:
11
Omit Elements on Declaration
12
C++ Array Size
▪ To get the size of an array, you can use the sizeof() operator:
▪ Why did the result show 20 instead of 5, when the array contains 5
elements?
▪ It is because the sizeof() operator returns the size of a type in bytes.
▪ You learned from our Recap lessons that an int type is usually 4 bytes,
so from the example above, 4 x 5 (4 bytes x 5 elements) = 20 bytes.
13
C++ Array size
▪ To find out how many elements an array has, you have to divide
the size of the array by the size of the data type it contains:
14
Activity 3
▪ This is an activity you must complete before you come to the next
class.
▪ Loop through the myNumbers array using the sizeof() function,
15
Passing An Array To A Function
▪ The code that is used to pass an array to a function includes the array’s
element type and its name. This is illustrated in the next example. It
includes two functions that process arrays. In both parameter lists, the
array a[] is declared in the parameter list as double a[].
▪ The actual number of elements has to be passed by means of a
separate integer variable. When a function is passed an array this way,
it is actually passed only the address of the memory cell where the
array starts. This value is represented by the array’s name a. The
function can then change the contents of the array by directly
accessing the memory cells where the array’s elements are stored. So,
although the name of the array is passed by value, its elements can be
changed just as if they had been passed by reference.
16
Example
17
Activity 4
▪ Your task is to create a program that uses this function to add the first n
elements of an array based on input from the user.
18
Some common errors
19
Multi-Dimensional Arrays
20
Continued...
▪ As with ordinary arrays, you can insert values with an array literal - a
comma-separated list inside curly braces. In a multi-dimensional
array, each element in an array literal is another array literal.
21
Continued...
22
Access the Elements of a Multi-
Dimensional Array
23
Loop Through a Multi-Dimensional Array
24
Example
25
Why Multi-Dimensional Arrays?
26
Activity 5
▪ Loop through the array with student records in the previous example
and display the student’s information.
27
C++ Strings
▪ Reminder:
– Strings are used for storing text.
28
C-String
▪ If you follow the rule of array initialization, then you can write the above
statement as follows −
29
Memory Representation
▪ Actually, you do not place the null character at the end of a string literal.
The C++ compiler automatically places the ‘\0' at the end of the string
when it initializes the array.
30
Example:
31
The null character (\0)
▪ The null character indicates the end of the string. Such strings are
called null-terminated strings.
▪ If you don’t have the null character at the end of a C-string you will
get a garbage output added when you display your output .
▪ Example:
– Remove the \0 element found at the end of the greeting string found on line 8
32
Accepting C-Strings from the user
33
Reading embedded blanks
34
Reading Multiple lines
▪ To accept multiple lines, we use the getline() function. Which is a pre-
defined function defined in a <string.h> header file used to accept a
line or a string from the input stream until the delimiting character is
encountered.
▪ The cin.getline() function takes three parameters as follows:
– cin.getline(charArray,length,delimiter)
35
Example
36
The <cstring> library
37
Example:
38
Activity 6
▪ Step one : Create a C-string called myWords that has a size of 150.
▪ Step two : Accept multi line input from the user and store it in
myWords. Your Delimiter should be the number 5.
▪ Step three: Display what is stored in my words as an output.
▪ Step four: Display what is stored in myWords reversed as an output
39
The String class
40
String Constants
41
Activity 6
42
String Concatenation
43
Append a String
44
C++ Numbers and Strings
▪ WARNING!
▪ C++ uses the + operator for both addition and
concatenation.
▪ Numbers are added. Strings are concatenated.
45
C++ String Length
▪ You might see some C++ programs that use the size() function to get
the length of a string. This is just an alias of length(). It is completely
up to you if you want to use length() or size():
46
Access Strings
▪ Note: String indexes start with 0: [0] is the first character. [1] is the
second character, etc.
47
Change String Characters
48
C++ Special Characters
49
Example
Note:
50
Reading whitespaces and Multiple lines
51
Using getline()
▪ we often use the getline() function to read a line of text. It takes cin
as the first parameter, and the string variable as second.
52
Reading multiple lines
53
Activities
54