Anda di halaman 1dari 40

Chapter 10:

Characters, CStrings, and More About the string Class

Copyright 2012 Pearson Education, Inc.

10.1
Character Testing

Copyright 2012 Pearson Education, Inc.

Character Testing
require cctype header file
FUNCTION isalpha isalnum isdigit islower isprint ispunct isupper isspace MEANING true if arg. is a letter false !ther"ise true if arg. is a letter !r digit false !ther"ise true if arg. is a digit 0#$ false !ther"ise true if arg. is l!"ercase letter false !ther"ise true if arg. is a %rinta&le character false !ther"ise true if arg. is a %unctuati!n character false !ther"ise true if arg. is an u%%ercase letter false !ther"ise true if arg. is a "hites%ace character false !ther"ise

Copyright 2012 Pearson Education, Inc.

Fr!' (r!gra' 10#1

Copyright 2012 Pearson Education, Inc.

10.)
Character Case C!n*ersi!n

Copyright 2012 Pearson Education, Inc.

Character Case C!n*ersi!n


+equire cctype header file Functi!ns,
toupper, if char argu'ent is l!"ercase letter return u%%ercase equi*alent- !ther"ise return in%ut unchanged char ch1 = 'H'; char ch2 = 'e'; char ch3 = '!'; cout << toupper(ch1); displays 'H' cout << toupper(ch2); displays '!' cout << toupper(ch3); displays '!'
Copyright 2012 Pearson Education, Inc.

Character Case C!n*ersi!n


Functi!ns,
tolower, if char argu'ent is u%%ercase letter return l!"ercase equi*alent- !ther"ise return in%ut unchanged char ch1 = 'H'; char ch2 = 'e'; char ch3 = '!'; cout << tolower(ch1); displays 'h' cout << tolower(ch2); displays 'e' cout << tolower(ch3); displays '!'

Copyright 2012 Pearson Education, Inc.

10..
C#/trings

Copyright 2012 Pearson Education, Inc.

C#/trings
C#string, sequence !f characters st!red in ad0acent 'e'!r1 l!cati!ns and ter'inated &1 "#$$ character /tring literal 2string c!nstant3, sequence !f characters encl!sed in d!u&le qu!tes 4 4 , %Hi there!%
H i t h e r e ! &'

Copyright 2012 Pearson Education, Inc.

C#/trings
Arra1 !f chars can &e used t! define st!rage f!r string, const int ()*! = 2'; char city+()*!,; 5ea*e r!!' f!r "#$$ at end Can enter a *alue using cin !r -6 In%ut is "hites%ace#ter'inated 6 N! chec7 t! see if en!ugh s%ace

F!r in%ut c!ntaining "hites%ace and t! c!ntr!l a'!unt !f in%ut use cin.getline()
Copyright 2012 Pearson Education, Inc.

Copyright 2012 Pearson Education, Inc.

10.8
5i&rar1 Functi!ns f!r 9!r7ing "ith C#/trings

Copyright 2012 Pearson Education, Inc.

5i&rar1 Functi!ns f!r 9!r7ing "ith C#/trings


+equire the cstring header file Functi!ns ta7e !ne !r '!re C#strings as argu'ents. Can use,
6 C#string na'e 6 %!inter t! C#string 6 literal string

Copyright 2012 Pearson Education, Inc.

5i&rar1 Functi!ns f!r 9!r7ing "ith C#/trings


Functi!ns,
/ strlen(str), returns length !f C#string str
char city+()*!, = %0issoula%; cout << strlen(city); prints 1

/ strcat(str12 str2), a%%ends str2 t! the end !f str1


char location+()*!, = %0issoula2 %; char state+3, = %03%; strcat(location2 state); location now has %0issoula2 03%
Copyright 2012 Pearson Education, Inc.

5i&rar1 Functi!ns f!r 9!r7ing "ith C#/trings


Functi!ns,
/ strcpy(str12 str2), c!%ies str2 t! str1
const int ()*! = 2'; char fname+()*!, = %0aureen%2 name+()*!,; strcpy(name2 fname);

N!te, strcat and strcpy %erf!r' n! &!unds chec7ing t! deter'ine if there is en!ugh s%ace in recei*ing character arra1 t! h!ld the string it is &eing assigned.
Copyright 2012 Pearson Education, Inc.

C#string Inside a C#string


Functi!n,
/ strstr(str12 str2), finds the first !ccurrence !f str2 in str1. +eturns a %!inter t! 'atch !r "#$$ if n! 'atch.
char ri4er+, = %5a6ash%; char word+, = %a6a%; cout << strstr(state2 word); displays %a6ash%

Copyright 2012 Pearson Education, Inc.

10.:
C#/tring;Nu'eric C!n*ersi!n Functi!ns

Copyright 2012 Pearson Education, Inc.

/tring;Nu'eric C!n*ersi!n Functi!ns


require cstdli6 header file
FUNCTION atoi atol atof itoa (A+AMETE+ C#string C#string C#string int2C#string int ACTION c!n*erts C#string t! an int *alue returns the *alue c!n*erts C#string t! a long *alue returns the *alue c!n*erts C#string t! a dou6le *alue returns the *alue c!n*erts 1st int %ara'eter t! a C#string st!res it in )nd %ara'eter. .rd %ara'eter is &ase !f c!n*erted *alue

Copyright 2012 Pearson Education, Inc.

/tring;Nu'eric C!n*ersi!n Functi!ns


int i"um; long l"um; dou6le d"um; char int7har+1',; i"um = atoi(%1238%); puts 1238 in i"um l"um = atol(%9:;1%); puts 9:;1 in l"um d"um = atof(%39.;%); puts 39.; in d"um itoa(i"um2 int7har2 1); puts the string %2322% (6ase 1 for 12381') in int7har

Copyright 2012 Pearson Education, Inc.

/tring;Nu'eric C!n*ersi!n Functi!ns # N!tes


if C#string c!ntains n!n#digits results are undefined
6 functi!n 'a1 return result u% t! n!n#digit 6 functi!n 'a1 return 0

< itoa d!es n! &!unds chec7ing 6 'a7e sure there is en!ugh s%ace t! st!re the result
Copyright 2012 Pearson Education, Inc.

10.<
Writing Your Own C-String Handling unctions

10.<
9riting =!ur O"n C#/tring >andling Functi!ns

Copyright 2012 Pearson Education, Inc.

9riting =!ur O"n C#/tring >andling Functi!ns


?esigning C#/tring >andling Functi!ns
6 can %ass arra1s !r %!inters t! char arra1s 6 Can %erf!r' &!unds chec7ing t! ensure en!ugh s%ace f!r results 6 Can antici%ate une@%ected user in%ut

Copyright 2012 Pearson Education, Inc.

Fr!' (r!gra' 10#$

Copyright 2012 Pearson Education, Inc.

Fr!' (r!gra' 10#10

Copyright 2012 Pearson Education, Inc.

10.A
M!re A&!ut the CBB string Class

Copyright 2012 Pearson Education, Inc.

The CBB string Class


/%ecial data t1%e su%%!rts "!r7ing "ith strings =include <string Can define string *aria&les in %r!gra's,
string first"ame2 last"ame;

Can recei*e *alues "ith assign'ent !%erat!r,


first"ame = %>eorge%; last"ame = %5ashington%;

Can &e dis%la1ed *ia cout


cout << first"ame << % % << last"ame;

Copyright 2012 Pearson Education, Inc.

Copyright 2012 Pearson Education, Inc.

In%ut int! a string O&0ect


Use cin -- t! read an ite' int! a string,
string first"ame; cout << %!nter your first name? %; cin -- first"ame;

Copyright 2012 Pearson Education, Inc.

Copyright 2012 Pearson Education, Inc.

In%ut int! a string O&0ect


Use getline functi!n t! %ut a line !f in%ut %!ssi&l1 including s%aces int! a string,
string address; cout << %!nter your address? %; getline(cin2address);

Copyright 2012 Pearson Education, Inc.

string C!'%aris!n
Can use relati!nal !%erat!rs directl1 t! c!'%are string !&0ects,
string str1 = %>eorge%2 str2 = %>eorgia%; if (str1 < str2) cout << str1 << % is less than % << str2;

C!'%aris!n is %erf!r'ed si'ilar t! strcmp functi!n. +esult is true !r false

Copyright 2012 Pearson Education, Inc.

Copyright 2012 Pearson Education, Inc.

Other ?efiniti!ns !f CBB strings


!e"inition string name; string myname(%7hris%); string yourname(myname); string aname(myname2 3); string 4er6(myname2322); Meaning defines an e'%t1 string !&0ect defines a string and initialiCes it defines a string and initialiCes it defines a string and initialiCes it "ith first . characters !f myname defines a string and initialiCes it "ith ) characters fr!' myname starting at %!siti!n . defines string and initialiCes it t! : '@'s

string noname('@'2 9);

Copyright 2012 Pearson Education, Inc.

string O%erat!rs
O(E+ATO+ -<< = A= A +, -2 -=2 <2 <=2 ==2 != MEANING e@tracts characters fr!' strea' u% t! "hites%ace insert int! string inserts string int! strea' assigns string !n right t! string !&0ect !n left a%%ends string !n right t! end !f c!ntents !n left c!ncatenates t"! strings references character in string using arra1 n!tati!n relati!nal !%erat!rs f!r string c!'%aris!n. +eturn true !r false

Copyright 2012 Pearson Education, Inc.

string O%erat!rs
string word12 phrase; string word2 = % Bog%; cin -- word1; user enters %Hot 3amale% word1 has %Hot% phrase = word1 A word2; phrase has %Hot Bog% phrase A= % on a 6un%; for (int i = '; i < 1:; iAA) cout << phrase+i,; displays %Hot Bog on a 6un%

Copyright 2012 Pearson Education, Inc.

Copyright 2012 Pearson Education, Inc.

string Me'&er Functi!ns


Are &ehind 'an1 !*erl!aded !%erat!rs Categ!ries,
6 assign'ent, assign2 copy2 data 6 '!dificati!n, append2 clear2 erase2 insert2 replace2 swap 6 s%ace 'anage'ent, capacity2 empty2 length2 resiCe2 siCe 6 su&strings, find2 su6str 6 c!'%aris!n, compare

/ee Ta&le 10#A f!r a list !f functi!ns

Copyright 2012 Pearson Education, Inc.

string Me'&er Functi!ns


string word12 word22 phrase; cin -- word1; word1 is %Hot% word2.assign(% Bog%); phrase.append(word1); phrase.append(word2); phrase has %Hot Bog% phrase.append(% with mustard relish%2 13); phrase has %Hot Bog with mustard% phrase.insert(12 %on a 6un %); cout << phrase << endl; displays %Hot Bog on a 6un with mustard%

Copyright 2012 Pearson Education, Inc.

Copyright 2012 Pearson Education, Inc.

Anda mungkin juga menyukai