You are on page 1of 33

BITG 1113:

Array (Part 2) & String

LECTURE 9

1
Objectives :
To know how to use array as function
parameter.
To understand basic concepts of string.
To know some functions of string.

2
Arrays and Functions

• Using function to process arrays in large program


– By passing individual elements
• Same as passing any ordinary variable to a
function

– By passing the whole array


• When we need the function to operate on the
whole array

3
Passing individual elements
• Passing individual element
– Array element must matches the function
parameter type

– When as a value parameter, function cannot


change the value of the element in the calling
function

– E.g : a function, print_square receives an


integer and prints its square, using an array, we
can loop through the array and pass each element
in turn to print_square

4
Example - Passing individual element

5
Passing the whole array

• Passing the whole array


– When we use large arrays in functions, by passing
each value we need an extra memory to do so

– E.g : if an array containing 20k elements were


passed by value to a function, another 20k
elements would have to be allocated in the
function and each element would have to be
copied from one array to another

• Instead of passing the whole array, c++ passes the


address of the array
6
– An array name is the address of the first element in
the array

– Because of the name of array is in fact its address,


passing an array name allows the called function to
refer to the array back in the calling function

– Two rules associated with passing the whole array


• The function must be called by passing only the
name of the array
• In the function definition, the formal parameter
must be an array type, the size of the array doesn’t
need to be specified

– A function can change the elements in array by


passing the array name without the constant modifier.
7
Passing the whole arrays for updating

8
Passing array as constants

9
Passing a two-dimensional array to function

• Passing an individual element


- Pass the individual element by indexing the array name with
the row number and the column number

• Passing a row
– Pass the whole row by indexing the array name with only the
row number

• Passing the whole array


– Use the array name as the actual parameter

10
Passing an individual element

#include <iostream>
using namespace std;
void print(int a);

 
void main()
{
int i,j,ary1[4][3]={{1,2,3},{2,4,6},{3,6,9},{3,2,1};

for(int i=0; i<4; i++){


for(int j=0; j<3; j++)
print(ary1[i][j]);
cout<<endl;

}
}

void print(int a)
{
cout<<a<<“\t“; 11
}
Passing a row

12
Passing the whole array

13
String (Character Array)

• String/Character Array : An array whose components are


of type char.
• Texts are examples of string : ”Hello there!”, ”UTeM”, etc.

• From the definition of string there is a difference between


’A’ and ”A”.
• The first one is character A; the second one is string A.

14
String (Character Array)

• To store ’A’ , we need only one memory cell of type char.

• To store ”A”,we need two memory cells of type char; one for
’A’ and one for ’\0’. (string – has extra memory location for
delimiter)
• String is null terminated. In C++, the null character is
represented by ’\0, called delimiter, which is nonprintable.
Therefore ”A” represents two character : ’A’ and ’\0’.

15
String (Character Array)
• Consider the following statement:
char name [16];
• This statement declares an array name of 16 components of
type char. Because string is null terminated and name has 16
components, the largest string that can be stored in name is of
length 15. If you store a string of length 10 in name, the first 11
components of name are used and the last 5 are left unused.
• Other ways of string declaration, (with initialization):
char name[16] = {‘J’,’o’,’h’,’n’,’\0’};
char name[16] = ”John”;//size of name is 16
char name[ ] = ”John”;//size of name is 5

16
String Data Manipulation

Manipulate Data

• Moving a character – use assignment


Example: name[0]=’J’; name[1]=’o’;
name[2]=’h’; name[3]=’n’;
• Moving a string – use function call to read string.
Remember! Below are errors if you are using string.
Mystring = “Hello”; or Mystring [ ] = “Hello”;

17
Function For String
• There are many useful functions for manipulating string
data such as comparing string, searching string, and
determining the length of string.

• These functions are provided by the string handling


library from the standard library.

• You must include the <cstring> header file when using


functions from the string handling library.

18
Function For String

Read String

• cin >> operator shows that the compiler expects the


user to type a one-word string from the keyboard
(e.g. ”Diploma”).
• Therefore, if you want the user to type text that
includes space, (e.g. ”Diploma UTeM”) use
getline() function.
• Example :
cin.getline(name, 30, ‘\n’);
cin.getline(name, 30);
19
Function For String

Convert String to other types

• The cstdlib library provides three useful functions for


this purpose:
– atoi: converts c-string to int type.
– atol: converts c-string to long type.
– atof: converts c-string to float type.
• Example:
cout<<atoi(“4”)+atoi(“5”)<<endl;
• Output: 9

20
Function For String

Copy String

• strcpy() function - used to copy one string into another


string.
• This function takes two arguments:
- The first argument is the string that you are trying to
replace.
- The second argument is the new string that you want to
replace.

21
Function For String
Copy String

• Can be used to replace an existing string or to initialize a


string.
• Example :
char CarName1[20];
char CarName2[20]=“Honda City”;

1) strcpy(CarName1, "Toyota Camry");


cout << "Car Name: " << CarName1 ;
Output : Toyota Camry
2) strcpy(CarName1, CarName2);
cout << "Car Name: " << CarName1 ;
Output : Honda City

22
Function For String
Copy String

• Allows you to specify the number of characters that the


compiler would copy from the source string.
• Example :
char CarName1[20];
char CarName2[20]=“Honda City”;

1)strncpy(CarName1, "Toyota Camry“, 6);


cout << "Car Name: " << CarName1 ;
Output : Toyota
2)strncpy(CarName1, CarName2, 5);
cout << "Car Name: " << CarName1 ;
Output : Honda
23
Function For String
Length of String

• strlen() function is used to find the number of characters


of a string.
• The strlen() function takes one argument, which is the
string you are considering.
• The function returns the string number of characters.
• Example :
char CarName [20]=“Honda City”;
int length;

length = strlen(CarName);
cout << "Car Name: " << length;
Output : 10
24
Function For String
Combining the string

• If you have two strings, to append one to another, use the


strcat() function.
• The strcat() function takes two arguments. The second
argument, called the source string, is the string you want
to add to the first string; this first string is referred to as
the destination.

25
Function For String
• Example :
char faculty[20] = “FTMK ";
char university[13] = “KUTKM,MELAKA"; 

strcat(faculty, university);
cout << "\n\nAfter concatenating,”<< faculty
<< endl;
Output:
After concatenating,FTMK KUTKM,MELAKA

26
Function For String

Combining the string

• strcat() function considers all characters of the source


string.
• strncat() function allows you to specify the number of
characters from the source string that you want to
append to the destination string.
• This means that, if the source string has 12 characters,
you can decide to append only a set number of its
characters.

27
Function For String

• Example :
char faculty[20] = “FTMK ";
char university[13] = “KUTKM,MELAKA"; 

strncat(faculty, university,5);
cout << "\n\nAfter concatenating,”<< faculty
<<endl;
Output :
FTMK KUTKM

28
Function For String
Duplicate string

• The strdup() function is used to make a copy or create


a duplicate of that string.
• This function takes the string you want to duplication as
an argument, and returns the duplicated string. 
• Example :
char faculty1[20] = “FTMK ";
char faculty2[20];  

faculty2 = strdup(faculty1);
cout << "\n\nAfter : ” << faculty2;
Output :
FTMK

29
Function For String

Comparing string

• The strcmp() function compares two strings and


returns an integer as a result of its comparison.
• This function takes two strings, S1 and S2 and compares
them. It returns 
– A negative value : if S1 is less than S2
– 0 : if S1 and S2 are equal
– A positive value : if S1 is greater than S2 

30
Function For String
• Example :
char faculty[10] = “FTMK ";
char input[10];
int i; 

cout<<“Enter your faculty:”;


cin>>input;
i=strcmp(faculty, input);

if(i == 0)
{
cout<< “You are from FTMK”;
}
31
Function For String
Convert to lowercase

• strlwr() function is used to convert a string to


lowercase.
• This function takes, as argument, the string that needs to
be converted. During conversion, if a Latin character
were in uppercase, it would be converted to lowercase.
• Example :
char faculty1[10] = “FTMK ";
char faculty2[10];

faculty2 = strlwr(faculty1);
cout<<faculty2;
Output :
ftmk
32
Function For String
Convert to uppercase

• strupr() function is used to convert a string to


uppercase.
• Each lowercase character in the function’s argument, S,
would be converted to uppercase.
• Any character or symbol that is not in lowercase would
not be changed. 
• Example :
char faculty1[10] = “ftmk ";
char faculty2[10];

faculty2 = strupr(faculty1);
cout<<faculty2;
Output :
FTMK 33

You might also like