Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 1 Chapter 7 Enumerated Types and Strings Types.

Similar presentations


Presentation on theme: "Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 1 Chapter 7 Enumerated Types and Strings Types."— Presentation transcript:

1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 1 Chapter 7 Enumerated Types and Strings Types Lecturer: Mrs Rohani Hassan

2 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 2 Objectives F To learn how to declare a enumerated type and assign a value to it (§7.1). F To test and convert characters using the character functions (§7.9.1). F To store and access strings using arrays and pointers (§7.9.2). F To read strings from the keyboard (§7.9.3). F To process strings using the string functions (§7.9.4).

3 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 3 Enumerated Types enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY}; Once a type is defined, you can declare a variable of that type: Day day; The variable day can hold one of the values defined in the enumerated type. For example, the following statement assigns enumerated value MONDAY to variable day: day = MONDAY;

4 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 4 Enumerated Types As with any other type, you can declare and initialize a variable in one statement: Day day = MONDAY; Furthermore, C++ allows you to declare an enumerated type and variable in one statement. For example, enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY} day = MONDAY; TestEnumeratedType Run

5 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X F #include F using namespace std; F int main() F { F enum Day {MONDAY = 1, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY} day; F cout << "Enter a day (1 for Monday, 2 for Tuesday, etc): "; F int dayNumber; F cin >> dayNumber; F switch (dayNumber) { F case MONDAY: F cout << "Play soccer" << endl; F break; F case TUESDAY: F cout << "Piano lesson" << endl; F break; F case WEDNESDAY: F cout << "Math team" << endl; F break; F default: F cout << "Go home" << endl; F } F return 0; F } 5

6 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 6 Characters and C-Strings Strings are used often in programming. You have used string literals. A string is a sequence of characters. There are two ways to process strings in C++. One way is to treat strings as arrays of characters. This is known as pointer-based strings or C-strings. The other way is to process strings using the string class. The string class will be introduced in §9.14. This section introduces pointer-based strings.

7 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 7 Character Test Functions

8 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 8 Case Conversion Functions CharacterFunctionsRun

9 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 9 Storing and Accessing Strings A pointer-based string in C++ is an array of characters ending in the null terminator ('\0'), which indicates where a string terminates in memory. An array can be accessed via a pointer. So a string can also be accessed via a pointer, which points to the first character in the string. So you can declare a string variable using an array or a pointer. For example, the following two declarations are both fine: char city[7] = "Dallas"; // Option 1 char *pCity = "Dallas"; // Option 2

10 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 10 Pointer Syntax You can access city or pCity using the array syntax or pointer syntax. For example, cout << city[1] << endl; cout << *(city + 1) << endl; cout << pCity[1] << endl; cout << *(pCity + 1) << endl; each displays character a (the second element in the string).

11 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 11 Reading Strings You can read a string from the keyboard using the cin object. For example, see the following code: cout << "Enter a city: "; cin >> city; // read to array city cout << "You entered " << city << endl;

12 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 12 Reading Strings Using getline C++ provides the cin.getline function in the iostream header file, which reads a string into an array. The syntax of the function is: cin.getline(char array[], int size, char delimitChar) The function stops reading characters when the delimiter character is encountered or when the size - 1 number of characters are read. The last character in the array is reserved for the null terminator ('\0'). If the delimiter is encountered, it is read, but not stored in the array. The third argument delimitChar has a default value ('\n').

13 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 13 String Functions

14 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 14 String Examples CopyString Run CombineString Run CompareString Run StringConversion Run

15 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X Copy String F #include F using namespace std; F int main() F { F char s1[20]; F char s2[20] = "Dallas, Texas"; F char s3[20] = "AAAAAAAAAA"; F strcpy(s1, s2); F strncpy(s3, s2, 6); F s3[6] = '\0'; // Insert null terminator F cout << "The string in s1 is " << s1 << endl; F cout << "The string in s2 is " << s2 << endl; F cout << "The string in s3 is " << s3 << endl; F cout << "The length of string s3 is " << strlen(s3) << endl; F return 0; F } 15

16 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X Combine String F #include F using namespace std; F int main() F { F char s1[20] = "Dallas"; F char s2[20] = "Texas, USA"; F char s3[20] = "Dallas"; F strcat(strcat(s1, ", "), s2); F strncat(strcat(s3, ", "), s2, 5); F cout << "The string in s1 is " << s1 << endl; F cout << "The string in s2 is " << s2 << endl; F cout << "The string in s3 is " << s3 << endl; F cout << "The length of string s1 is " << strlen(s1) << endl; F cout << "The length of string s3 is " << strlen(s3) << endl; F return 0; F } 16

17 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X String Conversion F #include F using namespace std; F int main() F { F cout << atoi("4") + atoi("5") << endl; F cout << atof("4.5") + atof("5.5") << endl; F char s[10]; F itoa(42, s, 8); F cout << s << endl; F itoa(42, s, 10); F cout << s << endl; F itoa(42, s, 16); F cout << s << endl; F return 0; F } 17

18 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 18 Obtaining Substrings Often it is useful to obtain a substring from a string. But there is no such function in C++. You can develop your own function for extracting substrings. The header of the function can be specified as: char * substring(char *s, int start, int end) Substring.h Run TestSubstring

19 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 19 Checking Palindromes A string is a palindrome if it reads the same forward and backward. The words “mom,” “dad,” and “noon,” for example, are all palindromes. Run CheckPalindrome


Download ppt "Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 1 Chapter 7 Enumerated Types and Strings Types."

Similar presentations


Ads by Google