BITG 1113:: Array (Part 2) & String
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
3
Passing individual elements
• Passing individual element
– Array element must matches the function
parameter type
4
Example - Passing individual element
5
Passing the whole array
8
Passing array as constants
9
Passing a two-dimensional array to function
• Passing a row
– Pass the whole row by indexing the array name with only the
row number
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};
}
}
void print(int a)
{
cout<<a<<“\t“; 11
}
Passing a row
12
Passing the whole array
13
String (Character Array)
14
String (Character Array)
• 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
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.
18
Function For String
Read String
20
Function For String
Copy String
21
Function For String
Copy String
22
Function For String
Copy String
length = strlen(CarName);
cout << "Car Name: " << length;
Output : 10
24
Function For String
Combining the string
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
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
faculty2 = strdup(faculty1);
cout << "\n\nAfter : ” << faculty2;
Output :
FTMK
29
Function For String
Comparing string
30
Function For String
• Example :
char faculty[10] = “FTMK ";
char input[10];
int i;
if(i == 0)
{
cout<< “You are from FTMK”;
}
31
Function For String
Convert to lowercase
faculty2 = strlwr(faculty1);
cout<<faculty2;
Output :
ftmk
32
Function For String
Convert to uppercase
faculty2 = strupr(faculty1);
cout<<faculty2;
Output :
FTMK 33