1 / 18

CHƯƠNG 7 CHUỖI KÝ TỰ (String)

CHƯƠNG 7 CHUỖI KÝ TỰ (String). 1. Giới thiệu. Chuỗi là một mảng ký tự được kết thúc bằng ký tự null (‘’). Ký tự null (‘’) là ký tự dùng để kết thúc Chuỗi Hằng Chuỗi là Chuỗi được bao quanh bởi cặp dấu nháy đôi . Ví dụ : "Hello". 2. Khai báo và khởi tạo Chuỗi.

hallam
Download Presentation

CHƯƠNG 7 CHUỖI KÝ TỰ (String)

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. CHƯƠNG 7CHUỖI KÝ TỰ(String)

  2. 1. Giới thiệu • Chuỗi làmộtmảngkýtựđượckếtthúcbằngkýtự null (‘\0’). • Kýtự null (‘\0’) làkýtựdùngđểkếtthúcChuỗi • HằngChuỗi làChuỗi đượcbaoquanhbởicặpdấunháyđôi. Vídụ: "Hello"

  3. 2. KhaibáovàkhởitạoChuỗi Có 2 cáchkhaibáovàkhởitạoChuỗi • Cách 1: Dùngmảngmộtchiều • Vídụ: char str[12]; Trongkhaibáonày, bộnhớsẽcungcấp 12+1 bytes đểlưutrữnội dung củachuỗikýtựstr; byte cuốicùnglưutrữkýtự ‘\0’ đểkếtthúcchuỗi. char <Tênbiến> [Chiềudàitốiđa]

  4. 2. KhaibáovàkhởitạoChuỗi. Cách 2: Dùng con trỏ • Vídụ: char *str; Trongkhaibáonày, bộnhớsẽdành 2 byte đểlưutrữđịachỉcủabiến con trỏstrđangchỉđến, chưacungcấpnơiđểlưutrữdữliệu. char *<Tênbiến>

  5. 2. KhaibáovàkhởitạoChuỗi. • Chuỗi kýtựgiốngnhưmảng do đóđểkhởitạomộtChuỗi kýtựvớinhữnggiátrịxácđịnhtacóthểthựchiệntươngtựnhưvớimảng. char <Biến>[ ]=<”HằngChuỗi ”>

  6. 2. KhaibáovàkhởitạoChuỗi. • Vídụ: char str[] = {‘H’, ’e’, ’l’, ’l’, ’o’, ’\0’}; char str[] = “Hello”; char *str = “Hello”;

  7. 3. NhậpChuỗi • ĐểnhậpdữliệuchobiếnChuỗi, tadùnghàm gets() củathưviệnstdio.h. • Hàm gets() đọccáckýtựtừbànphímvàotrongmảngtrỏđếnbởi s chođếnkhinhấn Enter. Kýtự null sẽđượcđặtsaukýtựcuốicùngcủaChuỗi nhậpvàotrongmảng. • Hoặctacóthểdùngcin >> s; char *gets(char *s);

  8. 4. XuấtChuỗi • ĐểxuấtChuỗi ramànhình, tadùnghàm puts() củathưviệnstdio.h.   • Hoặctacóthểdùngcout • cout << s; int puts(const char *s);

  9. Vídụ: #include <iostream.h> #include <stdio.h> int main() { char str[20]; cout<<"nhapchuoi:"; gets(str); cout<<"\nXuatchuoi:"; puts(str); return 0; }

  10. 5. CáchàmthaotáctrênChuỗi • strcpy(s1, s2): Sao chépChuỗi s2 vào s1 Vídụ: #include <iostream.h> #include <stdio.h> #include <string.h> void main() { char str1[20], str2[20]; cout<<"nhapchuoi 1:"; gets(str1); strcpy(str2,str1); cout<<"\nXuatchuoi 2:"; puts(str2); }

  11. 5. Cáchàmthaotáctrênchuỗi • strcat(s1, s2): Nốichuỗi s2 vàocuốiChuỗi s1 Vídụ: #include <iostream.h> #include <stdio.h> #include <string.h> void main() { char str1[20], str2[20]; cout<<"nhapchuoi 1:"; gets(str1); cout<<"\nhapchuoi 2:"; gets(str2); strcat(str1,str2); cout<<"\nXuatchuoisaukhinoi:"; puts(str1); }

  12. 5. CáchàmthaotáctrênChuỗi • strchr(s1, ch) : Trảvề con trỏđếnvịtríxuấthiệnđầutiêncủakýtựchtrongChuỗi s1 Vídụ: void main() { char *p, h, str1[20]; cout<<"nhapchuoi 1:”; gets(str1); cout<<"Nhapktumuontim:"; cin>>h; p= strchr(str1,h); if(p==NULL) cout<<"Khongtimthay "; else cout<<"Tim thay tai vi tri "<<(p-str1); }

  13. 5. CáchàmthaotáctrênChuỗi • strstr(s1, s2): Trảvề con trỏđếnvịtríxuấthiệnđầutiêncủaChuỗi s2 trong s1. Vídụ: void main() { char *p, str1[20], str2[20]; cout<<"nhapchuoi 1:"; gets(str1); cout<<"nhapchuoi 2:"; gets(str2); p= strstr(str1,str2); if(p==NULL) cout<<"Khongtimthay "; else cout<<"Tim thay tai vi tri "<<(p-str1); }

  14. 6. MảngcácChuỗi • MảngcácChuỗi làmộtmảngkýtựhaichiều. KíchthướccủachỉmụcthứnhấtlàsốChuỗi vàkíchthướccủachỉmụcthứhaixácđịnhchiềudàilớnnhấtcủamỗiChuỗi. Vídụ: char str[5][80]; Khaibáomộtmảngcủa5 Chuỗi, mỗiChuỗi cóchiềudàitốiđalà 79 kýtự.

  15. 6. MảngcácChuỗi • KhaibáovàkhởitạomảngcácChuỗi char arrayList[][length] = { constantString1, constantString2, ... constantStringN}; • Vídụ: char listOfPL[][10] = {“Pascal”, “C++”, “C#”};

  16. 6. MảngcácChuỗi Vídụ: void main() { char list[5][20]; for(inti=0; i<5; i++) { cout<<"name"<<i<<":“; cin>>list[i]; } for(int j=0; j<5; j++) cout<<" "<<list[j]; }

  17. 7. Mảng con trỏđếncácChuỗi • NgoàicáchdùngmảngkýtựhaichiềuđểlưutrữmảngcácChuỗi, tacóthểdùngmảngcủacác con trỏ. Mỗi con trỏsẽchứađịachỉcủaChuỗi • Vídụ: char *str[20];

  18. void main() { char *name[5]; for(inti=0 ; i<5 ; i++) name[i] = (char *)malloc(20);   for(inti=0 ; i<5 ; i++) { cout << "Input name " << i+1 <<": "; gets(name[i]); } cout << "List of names: "; for(inti=0 ; i<5 ; i++) cout << name[i] << ", "; }

More Related