1 / 41

第九章 字元與字串

第九章 字元與字串. 9.1 C 型態字串函數 9-2 9.1.1 取得字串長度 strlen 9-2 9.1.2 複製字串 strcpy 9-3 9.1.3 比較字串 strcmp 9-4 9.1.4 串接字串 strcat 9-5 9.2 C 型態字元函數 9-6 9.2.1 字元測試 9-7 9.2.2 大寫轉換小寫 tolower 9-20 9.2.3 小寫轉換大寫 toupper 9-21 9.3 字串與數值轉換函數 9-22. 9.3.1 轉成浮點數值 atof 9-22 9.3.2 轉成整數值 atoi 9-23

skylar
Télécharger la présentation

第九章 字元與字串

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. 第九章字元與字串

  2. 9.1C型態字串函數 9-2 9.1.1 取得字串長度strlen 9-2 9.1.2 複製字串strcpy 9-3 9.1.3 比較字串strcmp 9-4 9.1.4 串接字串strcat 9-5 9.2C型態字元函數 9-6 9.2.1 字元測試 9-7 9.2.2 大寫轉換小寫tolower 9-20 9.2.3 小寫轉換大寫toupper 9-21 9.3字串與數值轉換函數 9-22 9.3.1 轉成浮點數值atof 9-22 9.3.2 轉成整數值atoi 9-23 9.3.3 轉成長整數值atol 9-23 9.3.4 整數轉成字串itoa 9-24 9.4C++字串類別 9-26 9.4.1 建立C++字串 9-26 9.4.2 輸入C++字串 9-28 9.4.3 C++字串運算符號 9-29 9.4.4 C++ 字串陣列 9-32 9.4.5 C++字串類別成員 9-33 9 字元與字串

  3. 9.1C型態字串函數 • C++ 提供二種字串型態:一是使用char定義的C型態字串,另一則是使用string定義的C++ 型態字串。C型態字串在7.4節已經介紹過。 • 本節主要討論應用於C型態字串的函數,如取得字串長度(strlen)、複製字串(strcpy)、比較字串(strcmp)、串接字串(strcat)等。 • 至於C++ 型態字串則將於9.4節討論。

  4. 9.1.1 取得字串長度strlen • strlen函數是計算並傳回指定字串的位元組(byte)數。 • 範例 char fixstr[80] = "Ctype String"; //定義C型態字串 int fixlen = strlen(fixstr); //取得字串長度 #include <string.h> strlen (指定字串)

  5. 9.1.2 複製字串strcpy #include <string.h> strcpy (目的字串, 來源字串) • 範例 char source[80], target[80]; source[80] = "Ctype String"; //起始source字串 strcpy(target, source); //複製字串

  6. 9.1.3 比較字串strcmp #include <string.h> strcmp (字串1, 字串2) 傳回正數:表示字串1的ASCII碼大於字串2的ASCII碼 傳回零:表示二字串相等 傳回負數:表示字串1的ASCII碼小於字串2的ASCII碼 • 範例 char password[80] = "2002"; //定義並啟始密碼 char instring[80]; cin.getline (instring, 80, '\n'); //輸入字串 int flag = strcmp(password, instring); //flag=比較字串結果

  7. 9.1.3 cin.getline() • Getline是取得鍵盤輸入資料,包含空白,直到輸入「enter」為止。 • 範例 char sentence[80]; cin.getline (sentence, 80, '\n'); cin.getline(變數, 長度, ‘\n’)

  8. 9.1.4 串接字串strcat #include <string.h> strcat(字串1, 字串2) • 範例 char first[80] = "Sharon"; char last[80] = "Stone"; char full[160] = ""; strcat(full, first); //full="Sharon" strcat(full, " "); //full="Sharon " strcat(full, last); //full="Sharon Stone"

  9. 9.2C型態字元函數 • ctype.h標題檔包含C型態的字元函數,如字元測試、大寫轉成小寫(tolower)、與小寫轉成大寫(toupper)等函數。

  10. 9.2.1 字元測試 • 測試英數字元isalnum • 範例 int alnum = 0; char flexStr[] = "Ansi/Iso C++ 學習講堂 07/01/2002"; int len = strlen(flexStr); //取得字串長度 for (int i = 0; i <= len; i++) //字元檢查迴圈 if (isalnum(flexStr[i]) != 0) //若為英數字元 alnum++; //alnum = alnum+1 #include <ctype.h> isalnum (字元)

  11. 9.2.1 字元測試 (續) • 測試英文字元isalpha • 範例 int alpha = 0; char flexStr[] = "Ansi/Iso C++ 學習講堂 07/01/2002"; int len = strlen(flexStr); //取得字串長度 for (int i = 0; i <= len; i++) //字元檢查迴圈 if (isalpha(flexStr[i]) != 0) //若為英文字元 alpha++; //alpha = alpha + 1 #include <ctype.h> isalpha (字元)

  12. 9.2.1 字元測試 (續) • 測試控制字元iscntrl • 範例 int cntrl = 0; char flexStr[] = "Ansi/Iso C++ 學習講堂 07/01/2002"; int len = strlen(flexStr); //取得字串長度 for (int i = 0; i <= len; i++) //字元檢查迴圈 if (iscntrl(flexStr[i]) != 0) //若為控制字元 cntrl++; //cntrl = cntrl + 1 #include <ctype.h> iscntrl (字元)

  13. 9.2.1 字元測試 (續) • 測試數字字元isdigit • 範例 int digit = 0; char flexStr[] = "Ansi/Iso C++ 學習講堂 07/01/2002"; int len = strlen(flexStr); //取得字串長度 for (int i = 0; i <= len; i++) //字元檢查迴圈 if (isdigit(flexStr[i]) != 0) //若為數字字元 digit++; //digit = digit + 1 #include <ctype.h> isdigit (字元)

  14. 9.2.1 字元測試 (續) • 測試可顯示字元isgraph • 範例 int graph = 0; char flexStr[] = "Ansi/Iso C++ 學習講堂 07/01/2002"; int len = strlen(flexStr); //取得字串長度 for (int i = 0; i <= len; i++) //字元檢查迴圈 if (isgraph(flexStr[i]) != 0) //若為可顯示字元 graph++; //graph = graph + 1 #include <ctype.h> isgraph (字元)

  15. 9.2.1 字元測試 (續) • 測試小寫字元islower • 範例 int lower = 0; char flexStr[] = "Ansi/Iso C++ 學習講堂 07/01/2002"; int len = strlen(flexStr); //取得字串長度 for (int i = 0; i <= len; i++) //字元檢查迴圈 if (islower(flexStr[i]) != 0) //若為小寫字元 lower++; //lower = lower + 1 #include <ctype.h> islower (字元)

  16. 9.2.1 字元測試 (續) • 測試可列印字元isprint • 範例 int print = 0; char flexStr[] = "Ansi/Iso C++ 學習講堂 07/01/2002"; int len = strlen(flexStr); //取得字串長度 for (int i = 0; i <= len; i++) //字元檢查迴圈 if (isprint(flexStr[i]) != 0) //若為可列印字元 print++; //print = print + 1 #include <ctype.h> isprint (字元)

  17. 9.2.1 字元測試 (續) • 測試符號字元ispunct • 範例 int punct = 0; char flexStr[] = "Ansi/Iso C++ 學習講堂 07/01/2002"; int len = strlen(flexStr); //取得字串長度 for (int i = 0; i <= len; i++) //字元檢查迴圈 if (ispunct(flexStr[i]) != 0) //若為符號字元 punct++; //punct = punct + 1 #include <ctype.h> ispunct (字元)

  18. 9.2.1 字元測試 (續) • 測試空白字元isspace • 範例 int space = 0; char flexStr[] = "Ansi/Iso C++ 學習講堂 07/01/2002"; int len = strlen(flexStr); //取得字串長度 for (int i = 0; i <= len; i++) //字元檢查迴圈 if (isspace (flexStr[i]) != 0) //若為空白字元 space++; //space = space + 1 #include <ctype.h> isspace (字元)

  19. 9.2.1 字元測試 (續) • 測試大寫字元isupper • 範例 int upper = 0; char flexStr[] = "Ansi/Iso C++ 學習講堂 07/01/2002"; int len = strlen(flexStr); //取得字串長度 for (int i = 0; i <= len; i++) //字元檢查迴圈 if (isupper(flexStr[i]) != 0) //若為大寫字元 upper++; //upper = upper + 1 #include <ctype.h> isupper (字元)

  20. 9.2.1 字元測試 (續) • 測試16進位字元isxdigit • 範例 int xdigit = 0; char flexStr[] = "Ansi/Iso C++ 學習講堂 07/01/2002"; int len = strlen(flexStr); //取得字串長度 for (int i = 0; i <= len; i++) //字元檢查迴圈 if (isxdigit (flexStr[i]) != 0) //若為16進位字元 xdigit++; //xdigit = xdigit+1 #include <ctype.h> isxdigit (字元)

  21. 9.2.2 大寫轉換小寫tolower #include <ctype.h> tolower (字元) • 範例 char flexStr[] = "Success is never ending. Failure is never final."; int len = strlen(flexStr); //取得字串長度 for (int i = 0; i <= len; i++) //轉成小寫迴圈 flexStr[i] = tolower(flexStr[i]); //轉成小寫字元

  22. 9.2.3 小寫轉換大寫toupper #include <ctype.h> toupper (字元) • 範例 char flexStr[] = "Success is never ending. Failure is never final."; int len = strlen(flexStr); //取得字串長度 for (int i = 0; i <= len; i++) //轉成小寫迴圈 flexStr[i] = toupper(flexStr[i]); //轉成大寫字元

  23. Ex 19 • 由鍵盤輸入一英文字串,然後輸出大小寫互換的結果字串。

  24. 9.3字串與數值轉換函數 • 字串形式的數值(“3.14159”)是不能當做算數運算的資料,所以C++ 提供字串與數值間的轉換函數,例如字串轉成浮點數(atof)、字串轉成整數(atoi)、字串轉成長整數(atol)等函數,以及整數轉成字串(itoa)的函數。 123 ‘123’

  25. 9.3.1 轉成浮點數值atof • 範例 s = "-1998.12E-25 "; //定義字串 x = atof( s ); //轉浮點數x=-1.99812e-22 #include <stdlib.h> atof (字串)

  26. 9.3.2 轉成整數值atoi • 範例 s = " 686 pigs "; //定義字串 i = atoi( s ); //轉換成短整數,i=686 #include <stdlib.h> atoi (字串)

  27. 9.3.3 轉成長整數值atol • 範例 s = " 98686 dollars"; //定義字串 l = atol( s ); //轉換成長整數,l=98686 #include <stdlib.h> atol (字串)

  28. 9.3.4 整數轉成字串itoa • 範例 char intArray[10]; itoa(1234, intArray, 8); //1234轉成字串"2322" itoa(1234, intArray, 10); //1234轉成字串"1234" itoa(1234, intArray, 16); //1234轉成字串"4d2" #include <stdlib.h> itoa (數值, 字串, 基底)

  29. 9.4C++字串類別 • C++ 字串類別是一個抽象的資料型態,它不是C++ 原本內建的資料型態,如int或char。 • C++ 字串類別與字串類別函數是定義於C++ 的新型標題檔(不含 .h的標題檔)中,而C型態的字串標題檔(string.h)並沒有定義這些函數。 • 使用這些函數以前,必須插入C++ 新型的標題檔(string)。 • 在插入C++ 新型標題檔後(例如:#include <iostream>),必須加入(using namespace std;)敘述,來宣告程式中的函數是使用新型的C++ 型態標題檔,而不是使用舊型的C型態標題檔。

  30. 9.4.1 建立C++字串 #include <string>using namespace std; string 物件名稱; //第一式 string 物件名稱(“字串”); //第二式 string 物件名稱 = “字串”; //第三式 string 物件名稱(“字元”, 長度); //第四式 string 物件名稱(字串物件); //第五式 string 物件名稱(字串物件, 起始, 長度); //第六式

  31. 9.4.1 建立C++字串 (續) • 範例:C 型態字串 char *name = "JOHN"; char name[20] = "JOHN"; • 範例:C++ 型態字串 string s1; //宣告s1 string s2("JOHN ARCHER"); //s2 = "JOHN ARCHER" string s3 = "MARY ARCHER"; //s3 = "MARY ARCHER" string s4("A", 4); //宣告s4 = "AAAA" string s5(s2); //宣告s5 = "JOHN ARCHER" string s6(s2, 0, 4); //宣告s6 = "JOHN"

  32. 9.4.2 輸入C++字串 #include <iostream> using namespace std; getline (cin, 字串物件) • 範例 string s; getline(cin, s); //假設輸入"Hello world!" cout << s; //輸出Hello world!

  33. 9.4.3 C++字串運算符號

  34. 9.4.3 C++字串運算符號 (續) • 指定資料 • 範例 string s1, s2("Hello"); s1 = s2; //s1="Hello" s1 = "Hello world!"; //s1="Hello world!"

  35. 9.4.3 C++字串運算符號 (續) • 串接字串 • 範例 string s1("Hello"), s2(" world"), s3; s3 = s1 + s2; //s3="Hello world" string s4(s3 + "!"); //s4="Hello world!" s1 += "!"; //s1="Hello!"

  36. 9.4.3 C++字串運算符號 (續) • 比較字串 • 範例 string s1("ANSI/ISO C++"), s2("Visual C++"); if(s1 == s2) cout << "s1 == s2"; else cout << "s1 != s2";

  37. 9.4.4 C++ 字串陣列 #include <string> using namespace std; string 字串物件[長度]; • 範例 string s1[ ] = {"Java", "Assembly", "Delpha", "Basic", "Fortran", "Cobol"};

  38. 9.4.5 C++字串類別成員

  39. 9.4.5 C++字串類別成員 (續)

  40. Homework 8 • 寫一個C++程式,請User輸入First name & Last name(中間需用空格隔開),程式會輸出Hello 『Last name』!Your name is 『First name』. • 例如user輸入的是Vincent Chang,程式輸出Hello Chang!Your name is Vincent.

  41. 期末報告 • 使用C++程式語言寫一個文字型遊戲。 • 報告繳交:6月 • 展示:6月

More Related