Vogel's TEXTBOOK OF PRACTICAL ORGANIC CHEMISTRY 5th ED Revised - Brian S. Furniss.pdf
COIT29222-Structured Programming Lecture Week 08 Reading: Textbook (4 th Ed.), Chapter 4 Textbook...
-
Upload
cleopatra-lee -
Category
Documents
-
view
212 -
download
0
Transcript of COIT29222-Structured Programming Lecture Week 08 Reading: Textbook (4 th Ed.), Chapter 4 Textbook...
COIT29222-Structured Programming Lecture Week 08
Reading: Textbook (4th Ed.), Chapter 4 Textbook (6th Ed.), Chapter 7 Study Guide Book 2, Module 11
This week, we will cover the following topics:Introduction to Character Arrays
Declaring & Initialising Character ArraysAccessing Character Arrays
String FunctionsTwo Dimensional Arrays
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
Strings
Newer Compilerseg Visual C++ 6.0#include <string>string myName = “Sue Smith”;
Older Compilerseg Borland Turbo C++#include <string.h>char myName[20] = “Sue Smith”;
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 charactersconst int Size=20;
char Address[Size];
Declaring Character Arrays
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
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
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 errorchar Array2[3] = "22";//OKchar 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
Character Arrays-Example
The following program will print out “Bill”
#include <iostream.h>void main(){ char Name[]=“Bill";
for (int i=0; Name[i] != '\0'; i++)cout << Name[i];
cout << endl;}
Character Arrays-Example
The following program will print out “lliB”
#include <iostream.h>void main(){ char Name[]=“Bill";
for (int i=2; i>=0; i--)cout << Name[i];
cout << endl;}
Character Arrays-Example #include <iostream.h>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
String Input and OutputOutput is as expected, input is not…
#include <iostream.h>void main (void){ char Sentence[80] = "\0"; cout << ”Enter a sentence => "; cin.getline(Sentence,80); cout << ”Sentence: ” << Sentence << endl;}
cin >> stops accepting input when it meets a white space character – getline does not
String Input and OutputNotice that…
#include <iostream.h>void main (void){ char Sentence[80] = "\0"; cout << "Enter a sentence => "; cin.getline(Sentence,80); cout << ”Sentence: ” << Sentence << endl;}
up to 79 characters will be assigned to Sentence – position 79 will hold the null character
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)
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
string Library FunctionsAssignment 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 <string.h>
strcpy( ) Function Example
#include <iostream.h>#include <string.h>void main (void){ char Array1[] = "111"; char Array2[] = "222"; cout << "Array1: " << Array1 << endl; strcpy(Array1,Array2); cout << "Array1: " << Array1 << endl;}
Array1: 111Array1: 222
strcat( ) Function Example
#include <iostream.h>#include <string.h>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: 333Array3: 333222
strcmp( ) Function ExampleUsing 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 stringscout << "Array1(" << Array1 << ") "
<< Relationship << " Array2(" << Array2 << ")" << endl;
}
strcmp( ) Function ExampleThe program below
#include <iostream.h>#include <string.h>void Compare(char Array1[], char Array2[]);void main (void){
Compare("111","222");Compare("111","122");Compare("abc","ABC");Compare("ABC","ABC");Compare("ABCD","ABC");
}
strcmp( ) Function ExampleProduces 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)
ctype Library FunctionsOther 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 <ctype.h>
toascii( ) Function Example
#include <iostream.h>#include <ctype.h>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: 49code for 2 is: 50code for a is: 97code for A is: 65
toupper( ) Function Example
#include <iostream.h>#include <ctype.h>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: 65upper case of a is: Aupper case of 1 is: 1upper case of A is: A
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 0 column 1 column 2 row 0 matrix[0][0]
row 1row 2 matrix[2][2]
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.
Example-add matrix 1 and matrix 2
#include <iostream.h> 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;
} }
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