You are on page 1of 46

Bi 9: Xu

Ging vin: Hong Th ip Khoa Cng ngh Thng tin H Cng Ngh

Chapter 9
Strings

Copyright 2010 Pearson Addison-Wesley. All rights reserved

Mc tiu bi hc
Xu k t kiu mng Kiu xu k t ca C (xu C) Cc cng c x l k t c/ghi k t Hm thnh vin get, put putback, peek, ignore Lp chun string X l xu

DTH

INT2202

Gii thiu
Hai kiu xu k t: 1. Xu C Mng vi kiu c s l char nh du kt thc xu bng null, \0 K thut c tha k t C 2. Lp string S dng khun mu

DTH

INT2202

Xu C
Mng vi kiu c s l char Mi bin nh ch s l 1 k t Thm 1 k t: \0 Gi l k t null nh du kt thc xu Trong cc v d trc ta s dng xu C Hng gi tr "Hello" c lu dng xu C

DTH

INT2202

Bin kiu xu C
Mng k t:
char s[10]; Khai bo 1 bin kiu xu C c th cha ti a 9 k t + 1 k t null

Thng l mng cha y

Khai bo n ln lu xu kch thc ln nht cn ti nh du kt thc bng null Phi cha k t null

Khc bit duy nht vi mng chun:

DTH

INT2202

Vic lu tr xu C
Mt mng chun: char s[10]; Nu s cha xu k t "Hi Mom!", n c lu nh sau:

DTH

INT2202

Khi to xu C
C th khi to xu C:
char myMessage[20] = "Hi there."; Khng cn in y ton b mng Bc khi to t \0 cui

C th b qua kch thc mng:


char shortString[] = "abc"; T ng t kch thc bng chiu di xu trong ngoc kp cng 1 KHNG ging: char shortString[] = {a, b, c};

DTH

INT2202

Ch s trong xu C
Mt xu C l mt mng C th truy cp ti cc bin nh ch s ca xu C. char ourString[5] = "Hi"; ourString[0] l H ourString[1] l i ourString[2] l \0 ourString[3] l khng xc nh ourString[4] l khng xc nh

DTH

INT2202

Thao tc da trn ch s ca xu C
C th thao tc trn cc bin nh ch s char happyString[7] = "DoBeDo"; happyString[6] = Z; Hy cn thn! y \0 (null) b ghi bng Z! Nu null b ghi , xu C khng cn hot ng nh mt xu na! Khng lng c kt qu!

DTH

INT2202

Th vin
Khai bo xu C Khng cn th vin C++ no c N c sn trong C++ Cc thao tc Cn th vin <cstring> Thng c khai bo khi dng xu C Khi ngi vit chng trnh mun lm g trn xu C

DTH

INT2202

= v == trn xu C
Bin kiu xu C khng ging cc bin khc
Khng th gn hay so snh trc tip: char aString[10]; aString = "Hello"; // KHNG HP L!

Ch c th dng = khi khai bo kt hp khi to xu C!

Phi dng hm th vin cho php gn:


strcpy(aString, "Hello"); Hm c sn (trong <cstring>) t gi tr ca aString bng "Hello" Khng kim tra kch thc!

Ngi vit chng trnh phi kim sot. Ging nh cc thao tc khc trn mng!

DTH

INT2202

So snh cc xu C
Khng th dng ton t ==
char aString[10] = "Hello"; char anotherString[10] = "Goodbye"; aString == anotherString; // Khng c php!

Phi dng hm th vin:


if (strcmp(aString, anotherString)) cout << "Strings NOT same."; else cout << "Strings are same.";

DTH

INT2202

Th vin <cstring>: Display 9.1 Mt s hm trn xu C c sn trong <cstring> (1/2)

Full of string manipulation functions

DTH

INT2202

Th vin <cstring>: Display 9.1 Mt s hm trn xu C c sn trong <cstring> (2/2)

DTH

INT2202

Cc hm trn xu C: strlen()
Chiu di xu" Khi lm vic vi xu k t ta thng cn thng tin chiu di xu:
char myString[10] = "dobedo"; cout << strlen(myString); Tr v s lng k t Khng tnh null Vi v d trn: 6

DTH

INT2202

Cc hm trn xu C: strcat()
strcat() Ni xu":
char stringVar[20] = "The rain"; strcat(stringVar, "in Spain"); Kt qu: stringVar cha "The rainin Spain" Hy cn thn. B sung du cch nu cn

DTH

INT2202

i s v tham s kiu xu C
Nhc li: xu C l mng V vy tham s kiu xu C l tham s kiu mng
Xu C truyn vo hm c th b hm bin i

Cng nh vi mng, thng th ta truyn thm tham s kch thc


Hm cng c th dng \0 tm im kt thc xu Do kch thc khng thc s cn thit nu hm khng bin i xu Dng t kha const bo v i s kiu xu C

DTH

INT2202

Ghi xu C
Ta c th ghi xu C ra thit b xut (mn hnh) dng ton t chn << Tht ra ta lm vic ny ri: cout << news << " Wow.\n"; Trong news l mt bin kiu xu C C th l do ton t << c np chng cho xu C!

DTH

INT2202

c vo xu C
C th c d liu t thit b nhp (bn phm) vo xu C dng ton t trch >> Tuy nhin, c vn ny sinh Du trng c xem l k t phn cch (delimiter) Tab, cch, xu ng dng b b qua Vic c dng khi gp k t phn cch Hy ch kch thc ca xu C Phi ln cha xu nhp vo C++ khng cnh bo v vn ny

DTH

INT2202

V d c vo xu C
char a[80], b[80]; cout << "Enter input: "; cin >> a >> b; cout << a << b << "END OF OUTPUT\n"; Kt qu thc thi:
Enter input: Do be do to you! DobeEND OF OUTPUT Lu : phn gch di c nhp t bn phm

Xu C a c vo do Xu C b c vo be

DTH

INT2202

c mt dng vo xu C
C th ly ht dng k t ngi dng g t bn phm vo xu C Dng hm getline() c sn:
char a[80]; cout << "Enter input: "; cin.getline(a, 80); cout << a << "END OF OUTPUT\n"; Kt qu thc thi: Enter input: Do be do to you! Do be do to you!END OF INPUT

DTH

INT2202

V d: i s dng lnh
Chng trnh c gi t dng lnh (v d: UNIX shell, ni nhc lnh DOS) c th nhn i s V d: COPY C:\FOO.TXT D:\FOO2.TXT Lnh ny chy chng trnh c tn l COPY v truyn vo 2 tham s kiu xu C C:\FOO.TXT v D:\FOO2.TXT Vic x l u vo din ra bn trong chng trnh COPY (vi v d ny l sao tp c tn ch nh) i s c truyn vo hm main di dng mng cc xu C

DTH

INT2202

V d: i s dng lnh
Dng u ca main int main(int argc, char *argv[]) argc xc nh s lng i s truyn vo. N tnh c tn ca chng trnh. Do argc lun 1. argv l mt mng cc xu C. argv[0] lu tn ca chng trnh c gi argv[1] lu tham s th nht argv[2] lu tham s th hai v.v.

DTH

INT2202

Example: Command Line Arguments


// Test.cpp // In ra mn hnh cc i s truyn qua dng lnh int main(int argc, char* argv[]){ for(int i = 0; i < argc; i++){ cout << "Doi so " << i << ": " << argv[i] << endl; } return 0; }

V d gi chng trnh ln 1
> Test Doi so 0: Test

V d gi chng trnh ln 2 Gi chng trnh Test t ni nhc lnh


> Test Doi so Doi so Doi so hello world 0: Test 1: hello 2: world

DTH

INT2202

Bn thm v getline()
C th ch nh tng minh kch thc c vo: char shortString[5]; cout << "Enter input: "; cin.getline(shortString, 5); cout << shortString << "END OF OUTPUT\n"; Kt qu thc thi: Enter input: dobedowap dobeEND OF OUTPUT Bt buc ch c vo 4 k t K t cui l null c b sung t ng

DTH

INT2202

c/ghi k t
D liu c vo v ghi ra Tt c u c x l dng k t V d: s 10 ghi ra l 1 v 0 Vic chuyn i c thc hin t ng Dng cc tin ch bc thp Ta c ng c th dng cc tin ch bc thp ny

DTH

INT2202

Hm thnh vin get()


c tng k t mt Hm thnh vin ca i tng cin: char nextSymbol; cin.get(nextSymbol); c k t tip theo v a vo bin nextSymbol i s phi c kiu char Khng th l xu

DTH

INT2202

Hm thnh vin put()


Ghi tng k t mt ra thit b xut Hm thnh vin ca i tng cout: cout.put(a); In ch ci a ra mn hnh char myString[10] = "Hello"; cout.put(myString[1]); In ch ci e ra mn hnh

DTH

INT2202

Cc hm thnh vin khc


putback()
Sau khi ly d liu t lung vo bin, c th ta cn tr n li cho lung cin.putback(lastChar);

peek()
Tr v k t tip theo trong lung, nhng vn n li lung peekChar = cin.peek();

ignore()
B qua lung nhp ti khi no gp k t ch nh cin.ignore(1000, \n);
B qua ti a 1000 k t ti khi gp \n

DTH

INT2202

V d: Chng trnh dng hm putback


// Ngun: http://cplusplus.com/reference/iostream/istream/putback/ // istream putback #include <iostream> using namespace std; int main(){ char c; int n; char str[256]; cout << "Nhap vao 1 so nguyen duong hoac 1 tu: "; cin.get(c); if((c >= '0') && (c <= '9')){ cin.putback(c); cin >> n; cout << "Ban da nhap vao so " << n << endl; }else{ cin.putback(c); cin >> str; cout << "Ban da nhap vao tu " << str << endl; } cin.ignore(80, '\n'); cin.get(); return 0; }
DTH INT2202

Cc hm thao tc vi k t: Display 9.3 Mt s hm trong <cctype> (1/3)

DTH

INT2202

Cc hm thao tc vi k t: Display 9.3 Mt s hm trong <cctype> (2/3)

DTH

INT2202

Cc hm thao tc vi k t: Display 9.3 Mt s hm trong <cctype> (3/3)

DTH

INT2202

Lp chun string
nh ngha trong th vin:
#include <string> using namespace std;

Bin v biu thc string

X l nh vi cc kiu n

C th gn, so snh, cng:


string s1, s2, s3; s3 = s1 + s2; // Ni s3 = "Hello Mom!" // Gn Lu l xu C "Hello Mom!" c chuyn t ng sang kiu string!

DTH

INT2202

Display 9.4 Chng trnh dng lp string

DTH

INT2202

c/ghi vi lp string
Ging nh cc kiu khc! string s1, s2; cin >> s1; cin >> s2; Kt qu thc thi:
Ngi dng g vo: May the hair on your toes grow long and curly! s1 c vo gi tr "May" s2 c vo gi tr "the"

Ton t trch b qua du trng:

DTH

INT2202

getline() vi lp string
c ht dng ngi dng g vo: string line; cout << "Enter a line of input: "; getline(cin, line); cout << line << "END OF OUTPUT"; Kt qu thc thi: Enter a line of input: Do be do to you! Do be do to you!END OF INPUT Tng t nh cch dng getline() cho xu C

DTH

INT2202

Phin bn getline() khc


C th ch nh k t phn cch: string line; cout << "Enter input: "; getline(cin, line, ?); Nhn d liu ti khi gp du ? getline() tr v tham chiu string s1, s2; getline(cin, s1) >> s2; Cho kt qu l: (cin) >> s2;

DTH

INT2202

Li thng gp: Kt hp cc phng php c Hy cn thn khi kt hp cin >> var vi getline
int n; string line; cin >> n; getline(cin, line); Nu nhp vo:42 Hello hitchhiker.
Bin n s c t bng 42 line t bng xu rng!

cin >> n b qua du trng, \n li trong lung cho getline()!


DTH INT2202

X l xu vi lp string
C cc php ton nh cho xu C V hn th na! Hn 100 thnh vin ca lp string chun Mt s hm thnh vin: .length() Tr v di ca bin string .at(i) Tr v tham chiu ti k t v tr i

DTH

INT2202

Display 9.7 Hm thnh vin ca lp string chun (1/2)

DTH

INT2202

Display 9.7 Hm thnh vin ca lp string chun (1/2)

DTH

INT2202

Chuyn i gia xu C v i tng string


Chuyn i kiu t ng
T xu C thnh i tng string: char aCString[] = "My C-string"; string stringVar; stringVar = aCstring;
Hon ton hp l v ng!

aCString = stringVar;
Khng hp l! Khng th t ng chuyn thnh xu C

Phi dng lnh chuyn i tng minh: strcpy(aCString, stringVar.c_str());


DTH INT2202

Tm tt
Bin kiu xu C l mng k t
Vi k t null (\0) b sung cui

Xu C hot ng nh mng
Khng th gn, so snh nh cc bin n

Cc th vin <cctype> & <string> c nhiu hm x l gip x l xu d dng cin.get() c k t tip theo trong lung getline() c ht 1 dng trong lung Thao tc vi i tng string tin li hn xu C
DTH INT2202

Chun b bi ti
c chng 10 gio trnh: Con tr v Mng ng

DTH

INT2202