Presentation is loading. Please wait.

Presentation is loading. Please wait.

STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

Similar presentations


Presentation on theme: "STARTING OUT WITH STARTING OUT WITH Class 9 Honors."— Presentation transcript:

1

2 STARTING OUT WITH STARTING OUT WITH Class 9 Honors

3 2 Objectives Use the string and character input/output functions Use the character type functions in ctype.h Understand how to use the string conversion functions to validate numeric input

4 3 Objectives Use the C++ string class Explain the difference between strings created and used as cstrings versus string

5 4 Review of Pointer/Arrays/Strings char color1[ ]=“blue”; char * colorptr=“blue”; char color2[5]={‘b’,’l’,’u’,’e’,’\0’}; sizeof color1 5 sizeof colorptr 2 sizeof color2 5 cout << color1; blue cout << colorptr; blue cout << color2; blue

6 5 Review of Pointer/Arrays/Strings char color1[ ]=“blue”; char * colorptr=“blue”; char color2[5]={‘b’,’l’,’u’,’e’,’\0’}; color1[2] *colorptr *(color2+3) colorptr[1] colorptr + 2 color2 u b e l &u &b

7 6 Review of Pointer/Arrays/Strings char buffer[8]; char * ptr; cin >> buffer; CORRECT INCORRECT cin >> ptr; Does buffer contain an address? Does ptr contain an address? Does buffer point to allocated memory for storing characters? Does ptr point to allocated memory for storing characters? YES NO YESNO

8 7 Character Testing char Letter = ‘a’; if(isupper (Letter)) cout << “Letter is uppercase\n”; else cout << “Letter is lower case.\n”; i<strlen (colorptr);i++) colorptr[i] = toupper(colorptr[i]); Table 10-1 P. 566 convert char * colorptr = “blue” to uppercase using toupper for( int i = 0;

9 8 Character Testing int main() { char Customer[8]; cout << “Enter a customer number in the form”; cout << “LLLNNNN\n”; cin.getline(Customer,8); // user enters abc1234 if (TestNum(Customer)) cout << “That’s valid”; else cout << “not the proper format”; return 0; } P. 568 Pgrm 10-2

10 9 Character Testing int TestNum(char CustNum[]) { for (int Count=0; Count < 3; Count++) { if (!isalpha(CustNum[Count])) return false; } for (Count =3; Count < 7; Count++) { if (!isdigit(CustNum[Count])) return false;} return true; } P. 568 Pgrm 10-2

11 10 Character Testing int TestNum(char CustNum[]) { for (int Count=0; Count < 3; Count++) { if (!isalpha(CustNum[Count])) return false; } for (Count =3; Count < 7; Count++) { if (!isdigit(CustNum[Count])) return false;} return true; } P. 568 Pgrm 10-2

12 11 Exercise on Validating Numeric Input Prompt user for a number cout << “Enter a number\n”; Get response into a character array Check each character entered to see if it is a digit char buffer[6]; cin >> buffer; for (int i=0; i < strlen (buffer); i++) if(!isdigit(buffer[i])) { flag = 1; break; }

13 12 Exercise on Validating Numeric Input do { flag =0; cout << “Enter a number\n”; cin >> buffer; for (int i=0; i < strlen (buffer); i + +) if ( !isdigit (buffer[i]) ) { flag = 1; cout << “You have entered” << “an invalid number”; break;} } while (flag = = 1); int flag; char buffer[6];

14 13 atoiaccepts a string and converts the string to an integer and returns that value atofaccepts a string and converts the string to a float and returns than value int number; number = atoi(buffer); itoaConverts an integer to a string

15 14 C-String Functions from cstring Table 10-3 strlenaccepts a pointer to a string as an argument and returns the number of characters in the string excluding the null strcat accepts two pointers to two strings and appends the contents of the second string to the first string, altering the first string P.579

16 15 C-String Functions from cstring Table 10-3 strcpyaccepts two pointers to two strings as arguments and copies the second string on top of the first string, replacing the first string; the null is copied onto the end of the first string

17 16 String Functions from cstring Table 10-3 strncpy accepts two pointers to two strings and an integer as arguments and copies a specific number of characters from the second string on top of the first string, replacing the first string; the null is NOT copied onto the end of the first string; it is the programmers responsibility to put it there

18 17 String Functions from cstring Table 10-3 strstr accepts two pointers to two strings as arguments and searches for the first occurrence of the second string in the first string; a pointer to the first character of the substring is returned; if none is found, a NULL pointer is returned

19 18 String Copy cout << “enter your last name”; cin >> last; strcpy (name, last); name first last char name[13]; char last [6];char first [5]; // user enters JONES J JOES ES \0 N ON???????

20 19 String Concatenation name first last J JOES ES \0 N ON??????? cout << name; cout << last; Displays JONES cout << “enter your first name ”; // user enters TOMcin >> first; TOM\0

21 20 String Concatenation name first last J JOES ES \0 N ON??????? TOM strcat (name, ”,”);

22 21 String Concatenation name first last J JOES ES \0N ON? ????? TOM, strcat (name, ”,”); strcat (name,first);

23 22 String Concatenation name first last JOES\0N TOM strcat (name,first); JESON, strcat (name, ”,”); OM\0T??? ?

24 23 Searching for a substring inside a string char Array[ ] = “Four score and seven years ago”; char *StrPtr; StrPtr = strstr(Array,”seven”); cout << StrPtr; // displays seven years ago

25 24 “string” data type string movieTitle; // #include char bookTitle[20]; // #include movieTitle = “Wheels of Fury”; // or cin >> movieTitle; bookTitle = “From Here to Eternity”; // or cin >> bookTitle; strcpy(bookTitle,”From Here to Eternity”); cout << movieTitle << bookTitle; P.606-607

26 25 “string” data type string name1; // #include string name2; cout << “Enter a name (last name first):”; getline(cin,name1); cout << “Enter another name:”; getline(cin,name2); cout << “Here are the names sorted” << “alphabetically”; if (name1 < name2) cout << name1 << name2; else cout << name2 << name1; P.609 Prgm 10-15

27 26 “string” data type operations not available with cstring type character arrays +=Appends a copy of the string on the right to the string object on the left +Returns a string that is the concatenation of the two string operands []Implements array-subscript notation Relational Operators = == != P.611 Table 10-9

28 27 “string” data type operations not available with cstring type character arrays string str1, str2, str3; str1 = “ABC”; str2 = “DEF”; str3 = str1 + str2; cout << str1; // displays ABC cout << str2; // displays DEF cout << str3; // displays ABCDEF str3 += “GHI”; cout << str3; // displays ABCDEFGHI P.611 Pgrm 10-17

29 28 “string” data type operations string theString = “This is the time…”; cout << theString.at(3); theString.clear(); theString = “for all good men”; cout << theString.find(theString,’o’); cout << theString.length(); String result = theString.substr(4,3); cout << result; P.614 Table 10-10 s 1 16 all

30 29 Q & A

31 STARTING OUT WITH STARTING OUT WITH Conclusion of Class 9


Download ppt "STARTING OUT WITH STARTING OUT WITH Class 9 Honors."

Similar presentations


Ads by Google