1 / 30

Class 9 Honors

Class 9 Honors. 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. Objectives. Use the C++ string class <string>

raven
Download Presentation

Class 9 Honors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class 9 Honors

  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

  3. Objectives • Use the C++ string class <string> • Explain the difference between strings created and used as cstrings versus string

  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

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

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

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

  8. Character Testing P. 568 Pgrm 10-2 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; }

  9. P. 568 Pgrm 10-2 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; }

  10. P. 568 Pgrm 10-2 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; }

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

  12. Exercise on Validating Numeric Input int flag; char buffer[6]; 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);

  13. int number; number = atoi(buffer); atoi accepts a string and converts the string to an integer and returns that value atof accepts a string and converts the string to a float and returns than value itoa Converts an integer to a string

  14. P.579 C-String Functions from cstring Table 10-3 strlen accepts 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

  15. C-String Functions from cstring Table 10-3 strcpy accepts 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

  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

  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

  18. String Copy name J O N E S \0 ? ? ? ? ? ? ? last J O N E S \0 first char name[13]; charlast[6]; char first [5]; cout << “enter your last name”; cin >> last; // user enters JONES strcpy (name, last);

  19. String Concatenation name J O N E S \0 ? ? ? ? ? ? ? last J O N E S \0 first T O M \0 cout << name; cout << last; Displays JONES cout << “enter your first name”; cin >> first; //user enters TOM

  20. String Concatenation name J O N E S \0 ? ? ? ? ? ? ? last J O N E S \0 first T O M \0 strcat (name, ”,”);

  21. String Concatenation , strcat (name, ”,”); name J O N E S ? ? ? ? ? ? \0 last J O N E S \0 first T O M \0 strcat(name,first);

  22. String Concatenation name J O N E S , T O M \0 ? ? ? last J O N E S \0 first T O M \0 ? strcat (name, ”,”); strcat(name,first);

  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

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

  25. P.609Prgm 10-15 “string” data type string name1; // #include <string>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;

  26. “string” data type operations not available with cstring type character arrays P.611Table 10-9 += 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 < > <= >= == !=

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

  28. P.614Table 10-10 “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; s 1 16 all

  29. Q & A

  30. Conclusion of Class 9

More Related