Strings, Slide Fundamental Programming Strings
Strings, Slide 2 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, Slide 3 Newer Compilers eg Visual C #include string myName = “Sue Smith”; text pages
Strings, Slide 4 Older Compilers eg Borland Turbo C++ #include char myName[20] = “Sue Smith”; text pages
Strings, Slide 5 the following statement declares a variable that can hold a string containing up to five characters char Name[5]; Declaring Character Arrays
Strings, Slide 6 the following statements all 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
Strings, Slide 7 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
Strings, Slide 8 if no room is left for the null character, unexpected results can be expected char Array1[3] = "11"; char Array2[3] = "222"; char Array3[3] = "33"; given the above, the statement below may produce the following output cout << “Array2: “ << Array2; Initialising Character Arrays Array2: 22233
Strings, Slide 9 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
Strings, Slide 10 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
Strings, Slide 11 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)
Strings, Slide 12 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...
Strings, Slide 13 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
Strings, Slide 14 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
Strings, Slide 15 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:
Strings, Slide 16 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; }
Strings, Slide 17 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"); }
Strings, Slide 18 strcmp( ) Function Example produces the following output… comparison is made using the ASCII value on characters in the string – see page 49 in the textbook Array1(111) < Array2(222) Array1(111) < Array2(122) Array1(abc) > Array2(ABC) Array1(ABC) = Array2(ABC) Array1(ABCD) > Array2(ABC)
Strings, Slide 19 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
Strings, Slide 20 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
Strings, Slide 21 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
Strings, Slide 22 Summary Newer compilers use the string library Older compilers use string.h 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 string.h library functions exist to handle strings, eg strcat, strcpy, strlen, etc