+ All Categories
Home > Documents > STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

Date post: 03-Jan-2016
Category:
Upload: sandra-harper
View: 236 times
Download: 6 times
Share this document with a friend
30
STARTING OUT WITH Class 9 Class 9 Honors Honors
Transcript
Page 1: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

STARTING OUT WITH

STARTING OUT WITH

Class 9 HonorsClass 9 Honors

Page 2: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

2

ObjectivesObjectives

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

Page 3: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

3

ObjectivesObjectives

Use the C++ string class <string>

Explain the difference between strings created and used as cstrings versus string

Page 4: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

4

Review of Pointer/Arrays/StringsReview of Pointer/Arrays/Stringschar color1[ ]=“blue”;

char * colorptr=“blue”;

char color2[5]={‘b’,’l’,’u’,’e’,’\0’};

sizeof color1 5sizeof colorptr 2sizeof color2 5

cout << color1; bluecout << colorptr; bluecout << color2; blue

Page 5: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

5

Review of Pointer/Arrays/StringsReview of Pointer/Arrays/Stringschar color1[ ]=“blue”;char * colorptr=“blue”;char color2[5]={‘b’,’l’,’u’,’e’,’\0’};

color1[2] *colorptr *(color2+3)

colorptr[1] colorptr + 2 color2

ube

l&u&b

Page 6: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

6

Review of Pointer/Arrays/StringsReview 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

YES NO

Page 7: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

7

Character TestingCharacter 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;

Page 8: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

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

Page 9: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

9

Character TestingCharacter Testingint 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

Page 10: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

10

Character TestingCharacter 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

Page 11: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

11

Exercise on Validating Numeric InputExercise 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; }

Page 12: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

12

Exercise on Validating Numeric InputExercise 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];

Page 13: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

13

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

int number;

number = atoi(buffer);

itoa Converts an integer to a string

Page 14: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

14

C-String Functions from cstringC-String Functions from cstringTable 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

P.579

Page 15: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

15

C-String Functions from cstringC-String Functions from cstringTable 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

Page 16: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

16

String Functions from cstringString Functions from cstring

Table 10-3strncpy 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

Page 17: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

17

String Functions from cstringString 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

Page 18: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

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

J O E S

E S

\0

\0

N

O N ?? ? ? ? ? ?

Page 19: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

19

String Concatenation

name

first

last

J

J O E S

E S

\0

\0

N

O N ?? ? ? ? ? ?

cout << name;cout << last; Displays JONES

cout << “enter your first name”;

// user enters TOMcin >> first;

T O M \0

Page 20: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

20

String Concatenation

name

first

last

J

J O E S

E S

\0

\0

N

O N ?? ? ? ? ? ?

T O M \0

strcat (name, ”,”);

Page 21: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

21

String Concatenation

name

first

last

J

J O E S

E S

\0N

O N ?\0 ? ? ? ? ?

T O M \0

,

strcat (name, ”,”);

strcat (name,first);

Page 22: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

22

String Concatenation

name

first

last J O E S \0N

T O M \0

strcat (name,first);

J E SO N ,

strcat (name, ”,”);

O M \0T ?? ?

?

Page 23: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

23

Searching for a substring inside a stringSearching 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

Page 24: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

24

““string” data typestring” data typestring 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.606-607

Page 25: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

25

““string” data typestring” data typestring 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;

P.609Prgm 10-15

Page 26: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

26

““string” data type operationsstring” data type operations not available with cstring type character arrays not available with cstring type character arrays

+= Appends a copy of the stringon the right to the string object

on the left

+ Returns a string that is the concatenation of the two stringoperands

[] Implements array-subscript notation

Relational Operators < > <= >= == !=

P.611Table 10-9

Page 27: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

27

““string” data type operationsstring” data type operations not available with cstring type character arrays not available with cstring type character arrays

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.611Pgrm 10-17

Page 28: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

28

““string” data type operations 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.614Table 10-10

s

1

16

all

Page 29: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

29

Q & A

Page 30: STARTING OUT WITH STARTING OUT WITH Class 9 Honors.

STARTING OUT WITH

STARTING OUT WITH

Conclusion of Conclusion of Class 9Class 9


Recommended