300 likes | 423 Views
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>
E N D
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> • Explain the difference between strings created and used as cstrings versus string
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
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
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
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]);
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; }
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; }
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; }
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; }
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);
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
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
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
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
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
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);
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
String Concatenation name J O N E S \0 ? ? ? ? ? ? ? last J O N E S \0 first T O M \0 strcat (name, ”,”);
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);
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);
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
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;
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;
“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 < > <= >= == !=
“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
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