340 likes | 483 Views
第九章 字串. 本章學習重點. 9-1 字串的基本架構 9-2 字串的輸入及輸出 9-3 字串的函數. 9-1-1 字串的定義. 字串 (String) 在 C 語言中使用來儲存一串字元的一種資料結構 比字元陣列多出一個控制字元 『 』 ,用來表示字串的結束 字串 char a[ ]=”String”; char a[ ]={‘S’, ’t’, ’r’, ’i’, ’n’, ’g’, ’’}; 字元陣列 char b[ ]={‘S’, ’t’, ’r’, ’i’, ’n’, ’g’};. 9-1-2 字串的宣告 ( 一維陣列 ). 語法
E N D
本章學習重點 9-1 字串的基本架構 9-2 字串的輸入及輸出 9-3 字串的函數
9-1-1字串的定義 • 字串(String) • 在C語言中使用來儲存一串字元的一種資料結構 • 比字元陣列多出一個控制字元『\0』,用來表示字串的結束 • 字串 • char a[ ]=”String”; • char a[ ]={‘S’, ’t’, ’r’, ’i’, ’n’, ’g’, ’\0’}; • 字元陣列 • char b[ ]={‘S’, ’t’, ’r’, ’i’, ’n’, ’g’};
9-1-2 字串的宣告(一維陣列) • 語法 • char str[7]={‘H’, ’e’, ’l’, ’l’, ’o’, ’!’, ’\0’}; • 以類似字元陣列宣告 • char str[7]=”Hello!”; • 直接字串命名的方式 • char str[ ]=”Hello!”; • 編譯器會自動配置記憶體空間
範例 Ch9_2(1/2) ch9_2 輸出字串中每一個元素的內容及位址 1 #include<stdio.h> 2 void main( ) 3 { 4 int i = 0; 5 char str[ ] = "example"; 6 while(str[i]!=‘\0’) • { • printf("%c", str[i]); 9 printf(" %d\n", &str[i]); • i++; • } 12 }
ch9_2 輸出結果 (2/2) • 程式執行結果 • e 1245044 • x 1245045 • a 1245046 • m 1245047 • p 1245048 • l 1245049 • e 1245050
Column(行) Row (列) str[0][0] str[0][1] str[0][2] str[0][3] str[0][4] str[0][5] str[0][6] str[0][7] s t r i n g 1 \0 str[1][0] str[1][1] str[1][2] str[1][3] str[1][4] str[1][5] str[1][6] str[1][7] s t r i n g 2 \0 9-1-3 字串的宣告(二維陣列) str[2][8]={“string1”, ”string2”}; 在陣列中的擺放位置如下
範例 Ch9_3(1/2) ch9_3 建立一個存放一到四的字串陣列,並且將之輸出 1 #include<stdio.h> 2 void main( ) 3 { 4 int i; 5 char str[4][10]={“one”, ”two”, ”three”, ”four”}; 6 for(i=0; i < 4; i++) 7 printf(“%s\n”, str[i]); 8 }
Ch9_3 輸出結果 (2/2) • 程式執行結果 • one • two • three • four
宣告方式 宣告格式 結束條件 未指定欄寬 scanf(“%s”, str); 當讀到空白字元 指定欄寬 scanf(“%#s”, str); /*其中#表示一正整數*/ 1. 讀到空白字元時 2. 超過指定欄寬時 9-2 字串的輸入及輸出 9-2-1字串的輸入 • scanf( ) • gets( ) • 遇到空白字元時,並不會結束,其結束的條件為遇到結束符號『\0』
範例 Ch9_4(1/2) ch9_4 利用scanf( )輸入字串,並輸出 1 #include<stdio.h> 2 void main( ) 3 { 4 char str[10]; 5 printf("請輸入一字串:"); 6 scanf(“%6s”, str); /*指定欄寬為6個字元*/ 7 printf("輸入的字串為:%s", str); 8 }
ch9_4 輸出結果 (2/2) • 程式執行結果 • 請輸入一字串:123456789 • 輸入的字串為:123456
範例 Ch9_5(1/2) ch9_5 使用gets( )輸入字串,並將之輸出 1 #include<stdio.h> 2 void main( ) 3 { 4 char str[20]; 5 printf(“請輸入一字串:”); 6 gets(str); 7 printf(“輸入的字串為:%s”, str); 8 }
Ch9_5 輸出結果 (2/2) • 程式執行結果 • 請輸入一字串:This is a pen • 輸入的字串為:This is a pen
9-2 字串的輸入及輸出 9-2-2字串的輸出 • printf( ) • 一次能輸出多個字串 • 具格式化輸出的功能 • puts( ) • 一次只能輸出一個字串 • 不具格式化輸出的功能 • 每輸出一個字串,puts( )也將輸出一個跳列字
範例 Ch9_6 (1/2) ch9_6 以gets( )輸入字串,且以puts( )輸出字串 1 #include<stdio.h> 2 void main( ) 3 { 4 char input[20]; 5 puts(“請輸入字串:”); 6 gets(input); 7 puts(input); 8 }
Ch9_6 輸出結果 (2/2) • 程式執行結果 • 請輸入字串: • This is a pen • This is a pen
9-3 其它字串的處理函數 • 使用前,需加入string.h的標頭檔 • strlen( ) : 計算字串長度 • strcmp( ):比較兩個字串 • strcpy( ):將字串一複製到字串二 • strcat( ):合併字串一及字串二 • toupper( ):將字串內容轉換成大寫字母 • tolower( ):將字串內容轉換成小寫字母
範例 Ch9_9 (1/2) ch9_9 輸入一個字串,計算出該字串的長度 1 #include<string.h> /*因使用strlin( )函數,需叫用標頭檔*/ 2 #include<stdio.h> 3 void main( ) 4 { 5 char str[30]; 6 int number; 7 printf("請輸入一字串:"); 8 gets(str); 9 number = strlen(str); /*呼叫strlen( )函數,求出str的長度*/ 10 printf("\n字串長度為:%d", number); 11 }
Ch9_9 輸出結果 (2/2) • 程式執行結果 • 請輸入一字串:This is a pig • 字串長度為:13
9-3-2 strcmp( )函數 • 語法 • 整數型態變數= strcmp(字串陣列名稱一, 字串陣列名稱二); • 說明 • 整數型態變數 • 0代表兩個字串相同 • 負數及正數皆表示為不相同字串 • strcmp( ) • 會將字串一與字串二做一比較,看是否相同
範例 Ch9_10 (1/3) ch9_10 設計一個程式,檢查輸入的字串是否相同 1 #include<stdio.h> 2 #include<string.h> /*因使用strcmp( )函數,需叫用標頭檔*/ 3 void main( ) 4 { 5 char str1[20]; 6 char str2[20]; 7 int i; 8 printf("請輸入第一個字串:"); 9 gets(str1);
範例 Ch9_10 (2/3) 10 printf("請輸入第二個字串:"); 11 gets(str2); 12 i = strcmp(str1, str2); /*比較str1,str2字串是否相同*/ 13 if(i == 0) 14 printf("輸入的兩個字串為相同的字串!"); 15 else 16 printf("輸入的兩個字串為不相同的字串!"); 17 }
Ch9_10 輸出結果 (3/3) • 程式執行結果 • 請輸入第一個字串:computer • 請輸入第二個字串:computer • 輸入的兩個字串為相同的字串! • 請輸入第一個字串:computer • 請輸入第二個字串:mouse • 輸入的兩個字串為不相同的字串!
9-3-3 strcpy( )函數 • 語法 • strcpy(字串1, 字串2); • 說明 • 字串1:將要被複製成跟字串2相同內容的字串 • 字串2:將要複製給字串1的字串 • 此函數可將字串2變數的內容拷貝到字串1的內容(包含空字元)
範例 Ch9_11(1/2) ch9_11 使用者輸入一個字串,程式將以該字串覆蓋原程式內定的字串 1 #include<stdio.h> 2 #include<string.h> /*因使用strcpy( )函數,需叫用標頭檔*/ 3 void main( ) 4 { 5 char str1[30] = ”This is an original string”, str2[30]; 6 printf("請輸入一字串:"); 7 gets(str2); 8 printf(“原先內定字串1的內容為:%s\n”, str1); 9 strcpy(str1, str2); 10 printf("複製過後的字串1內容為:%s", str1); 11 }
Ch9_11 輸出結果 (2/2) • 程式執行結果 • 請輸入一字串:This is my input • 原先內定字串1的內容為:This is an original string • 複製過後的字串1內容為:This is my input
9-3-4 strcat( )函數 • 語法 • strcat(str1, str2); • 說明 • str2將會覆蓋住str1字串的結束字元'\0',且保留本身的結束字元'\0'
範例 Ch9_12(1/2) ch9_12 使用strcat( )函數,將兩個字串合併為一,並輸出 1 #include<stdio.h> 2 #include<string.h> /*因使用strcat( )函數,需叫用標頭檔*/ 3 void main( ) 4 { 5 char str1[20]=”Taiwan ”; 6 char str2[ ]=”University”; 7 printf(“合併前str1內容為:%s str2內容為:%s\n“, str1, str2); 8 strcat(str1, str2); 9 printf(“合併後str1內容為:%s str2內容為:%s\n”, str1, str2); 10 }
Ch9_12 輸出結果 (2/2) • 程式執行結果 • 合併前str1內容為:Taiwan str2內容為:University • 合併後str1內容為:Taiwan University str2內容為:University
9-3-5 toupper( )函數及tolower( )函數 • 語法 • 陣列名稱[索引值] = tolower( 陣列名稱[索引值] ); • 陣列名稱[索引值] = toupper( 陣列名稱[索引值] ); • 說明 • 需加入ctype.h的標頭擋 • toupper( )函數可以將字元強制轉換成大寫 • tolower( )函數則可以將字元強制轉換成小寫
範例 Ch9_13(1/3) ch9_13 將輸入的小寫字串轉換成大寫,並將輸入的大寫字串轉換成小寫 1 #include<stdio.h> 2 #include<ctype.h> /*使用toupper( )函數及tolower( )函數*/ 3 void main( ) 4 { 5 int i; 6 char str1[10]; 7 char str2[10]; 8 printf(“請輸入字串一: ”); 9 gets(str1); 10 printf(“請輸入字串二: ”); 11 gets(str2);
範例 Ch9_13(2/3) 12 for (i = 0 ; str1[i] != ’\0’; i++) 13 str1[i] = tolower( str1[i] ); 14 for (i = 0 ; str2[i] != ’\0’; i++) 15 str2[i] = toupper( str2[i] ); 16 printf(“大寫轉換成小寫:%s\n“, str1); 17 printf(“小寫轉換成大寫:%s “, str2); 18 }
Ch9_13 輸出結果 (3/3) • 程式執行結果 • 請輸入字串一:ABC • 請輸入字串二:efg • 大寫轉換成小寫:abc • 小寫轉換成大寫:EFG