Download presentation
Presentation is loading. Please wait.
Published byCleopatra Lee Modified over 9 years ago
1
COIT29222-Structured Programming Lecture Week 08 Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11 This week, we will cover the following topics: Introduction to Character Arrays Declaring & Initialising Character Arrays Accessing Character Arrays String Functions Two Dimensional Arrays
2
Array of Characters - Strings In C++, names, titles, messages, etc, can be held as an array of characters - the term string describes this type of data In C++, the null character marks the end of a string (the “character” with value 0 in the ASCII coding system - '\0' in C++) As such, character arrays to hold strings must be declared one element larger than the longest string they will hold
3
Strings Newer Compilers eg Visual C++ 6.0 #include string myName = “Sue Smith”; Older Compilers eg Borland Turbo C++ #include char myName[20] = “Sue Smith”;
4
The following statement declares an array of characters (MyName) that can hold a string containing up to five characters (including a null character) char MyName[5]; The following statements declare a constant (Size) and an array of characters (Address) that can hold a string containing up to ten characters const int Size=20; char Address[Size]; Declaring Character Arrays
5
The following statements declare an array holding four characters – the last being the null character char Company[4] = {'I','B','M','\0'}; char Company[4] = "IBM"; char Company[] = "IBM"; In last two, a C++ string is used to initialise the array – note use of double quotes Initialising Character Arrays
6
Initialising an array to the empty string can be achieved by: char Company[25] = "\0"; char Company[25] = {‘\0'}; The former is commonly used Initialising Character Arrays
7
If no room is left for the null character, unexpected results/error can be expected char Array1[3] = "111";//Some compilers will give an //error: array bounds overflow error char Array2[3] = "22";//OK char Array3[3] = "33“;//OK A character array can be printed with cout<< The statement below cout << “Array2: “ << Array2; will print the following output Initialising Character Arrays Array2: 22
8
Character Arrays-Example The following program will print out “Bill” #include void main() { char Name[]=“Bill"; for (int i=0; Name[i] != '\0'; i++) cout << Name[i]; cout << endl; }
9
Character Arrays-Example The following program will print out “lliB” #include void main() { char Name[]=“Bill"; for (int i=2; i>=0; i--) cout << Name[i]; cout << endl; }
10
Character Arrays-Example #include const int size=10; void main() { char Name[size]="ihgfedcba“; char temp; for (int i=0; i<size-1; i++) for (int j=0; j<size-2; j++) if (Name[j]> Name[j+1]) { temp=Name[j]; Name[j]=Name[j+1]; Name[j+1]=temp; } cout << Name << endl; } What does this program do? Output: abcdefghi
11
String Input and Output Output is as expected, input is not… #include void main (void) { char Sentence[80] = "\0"; cout "; cin.getline(Sentence,80); cout << ”Sentence: ” << Sentence << endl; } cin >> stops accepting input when it meets a white space character – getline does not
12
String Input and Output Notice that… #include void main (void) { char Sentence[80] = "\0"; cout "; cin.getline(Sentence,80); cout << ”Sentence: ” << Sentence << endl; } up to 79 characters will be assigned to Sentence – position 79 will hold the null character
13
getline( ) Function getline(InputArray,ArraySize,UntilChar); InputArray is a character array variable that will receive the input characters ArraySize is an integer expression stating the size of the array – (a maximum of ArraySize–1 characters will be assigned to array, followed by null) UntilChar is an optional parameter; if this character is encountered, input is terminated (‘\n’ is the default)
14
Other Issues We cannot simply assign the contents of one character array to another: CharArray1 = CharArray2; Or combine two character arrays CharArray1 = CharArray2 + CharArray3; Or compare two character arrays: if (CharArray1 == CharArray2) Use standard library functions instead
15
string Library Functions Assignment and comparison of character arrays is can be accomplished using strcpy(Str1,Str2); // Str2 is copied to Str1 strcat(Str1,Str2); // Str2 is joined to Str1 strcmp(Str1,Str2); // Str2 is compared to Str1 strlen(Str1); // length of Str1 To use these functions, your program must include the string library: #include
16
strcpy( ) Function Example #include void main (void) { char Array1[] = "111"; char Array2[] = "222"; cout << "Array1: " << Array1 << endl; strcpy(Array1,Array2); cout << "Array1: " << Array1 << endl; } Array1: 111 Array1: 222
17
strcat( ) Function Example #include void main (void) { char Array1[] = "111"; char Array2[] = "222"; char Array3[7] = "333"; cout << "Array3: " << Array3 << endl; strcat(Array3,Array2); cout << "Array3: " << Array3 << endl; } Array3: 333 Array3: 333222
18
strcmp( ) Function Example Using this Compare function void Compare(char Array1[], char Array2[]) { char Relationship = '='; // establish relationship between strings if (strcmp(Array1,Array2) < 0) Relationship = '<'; else if (strcmp(Array1,Array2) > 0) Relationship = '>'; // report relationship between strings cout << "Array1(" << Array1 << ") " << Relationship << " Array2(" << Array2 << ")" << endl; }
19
strcmp( ) Function Example The program below #include void Compare(char Array1[], char Array2[]); void main (void) { Compare("111","222"); Compare("111","122"); Compare("abc","ABC"); Compare("ABC","ABC"); Compare("ABCD","ABC"); }
20
strcmp( ) Function Example Produces the following output… Comparison is made using the ASCII value on characters in the string Array1(111) < Array2(222) Array1(111) < Array2(122) Array1(abc) > Array2(ABC) Array1(ABC) = Array2(ABC) Array1(ABCD) > Array2(ABC)
21
ctype Library Functions Other useful functions for handling characters and strings are: toascii(Char); // returns ACSII code for char toupper(Char); // returns upper case of char tolower(Char); // returns lower case of char To use these functions, your program must include the ctype library: #include
22
toascii( ) Function Example #include void main (void) { cout << "code for 1 is: " << toascii('1') << endl; cout << "code for 2 is: " << toascii('2') << endl; cout << "code for a is: " << toascii('a') << endl; cout << "code for A is: " << toascii('A') << endl; } code for 1 is: 49 code for 2 is: 50 code for a is: 97 code for A is: 65
23
toupper( ) Function Example #include void main (void) { cout << "upper case of a is: " << toupper('a') << endl; cout << "upper case of a is: " << char (toupper('a')) << endl; cout << "upper case of 1 is: " << char (toupper('1')) << endl; cout << "upper case of A is: " << char (toupper('A')) << endl; } upper case of a is: 65 upper case of a is: A upper case of 1 is: 1 upper case of A is: A
24
Declaring Two-Dimensional Arrays The following statement declares a two dimensional array (matrix) that can hold 9 integer values int matrix [3] [3]; matrix – rows & columns: column 0column 1column 2 row 0 matrix[0][0] row 1 row 2 matrix[2][2]
25
Initialising Two-Dimensional Arrays int matrix[4][3] = { { 5, 2, 3}, { 4, 2, 6}, { 1, 1, 1}, { 2, 1, 4} }; The declaration double matrix[4][3] = {5}; initialises matrix[0][0] to 5 and all remaining elements to 0. The declaration double matrix[4][3] = {1, 2, 3, 4}; initialises matrix[0] [0] to 1, matrix[0][2] to 3, matrix[1][0] to 4 and all remaining elements to zero.
26
Example-add matrix 1 and matrix 2 # include void main () { int matrix1[3][2] = { { 1, 2}, { 6, 2}, { 4, 5}}; int matrix2[3][2] = { { 2, 4}, { 3, 2}, { 4, 1}}; int matrix_res[3][2]; for (int rcount=0; rcount<3; rcount++) { for (int ccount=0;ccount<2; ccount ++) { matrix_res[rcount][ccount]=matrix1[rcount][ccount] + matrix2[rcount][ccount]; cout<< matrix_res[rcount][ccount]; } cout << endl; }
27
Summary Character arrays are used to hold strings Strings terminate in a null zero So arrays must be of sufficient size to hold the characters in the string, plus one Many library functions exist to handle strings, eg. strcat, strcpy, strlen, etc. Newer compilers use the string library Older compilers use string.h
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.