1 / 24

第 15 章 文字列処理

第 15 章 文字列処理. 15.1 文字列処理の基本 15.2 文字列処理用ライブラリ関数. 今日のポイント. str cpy 、 str cat 、 str len 、 str chr などの使い方をマスターする. 「 strcpy はなんて読む?」 「普通は ストリングコピー 」 Cのキーワードの読み方に悩んだら下記サイトを参考(前回紹介とは別サイト). http://www.okakogi.go.jp/People/miwa/program/c_lang/c_furoku.html. x[0]. x[1]. x[2]. x[3]. x[4].

dahlia
Download Presentation

第 15 章 文字列処理

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. 第15章 文字列処理 15.1 文字列処理の基本 15.2 文字列処理用ライブラリ関数

  2. 今日のポイント strcpy、strcat、strlen、strchr などの使い方をマスターする 「strcpyはなんて読む?」 「普通はストリングコピー」 Cのキーワードの読み方に悩んだら下記サイトを参考(前回紹介とは別サイト) http://www.okakogi.go.jp/People/miwa/program/c_lang/c_furoku.html

  3. x[0] x[1] x[2] x[3] x[4] x[18] x[19] 15.1文字列処理の基本 1.文字列は配列に格納 2.文字列の終わりは¥0 W i l l i 1000 1001 1002 ・ ・ ・ この2点が重要ポイント e ¥O プログラム例15.1.1〜15.1.3は授業ではパス. 文字列がわかって来たら、自分で見直すこと.

  4. 15.2文字列処理用ライブラリ関数15.2.1文字列のコピーと連結15.2文字列処理用ライブラリ関数15.2.1文字列のコピーと連結 文字列処理関数を使用するときは ヘッダに string.h を追加 を追加 #include<string.h>

  5. 15.2文字列処理用ライブラリ関数15.2.1文字列のコピー15.2文字列処理用ライブラリ関数15.2.1文字列のコピー 文字型の配列変数a,bを宣言 (30は特に意味なし。 十分なサイズにすれば良い) プログラム例15.2.1 より抜粋 char a[30], b[30]; char c[30] = {"University"}; strcpy(&a[0], "Japan "); strcpy(&b[0], &c[0]); 文字型配列変数cの初期値は "University"とする 文字列定数"Japan"を配列 変数aにコピー(=代入) 文字列cを文字列bに コピー(=代入) 後ろから前へ strcpy(string1, string2);

  6. 15.2文字列処理用ライブラリ関数15.2.1文字列のコピー15.2文字列処理用ライブラリ関数15.2.1文字列のコピー 後ろから前へ ほとんどのテキストで 下記のように記述 例 strcpy(d,s); strcpy(string1, string2); 文字型配列 string2 の 先頭アドレス(ポインタ) または“文字列定数” 文字型配列 string1 の 先頭アドレス(ポインタ) プログラム例 15.2.1のように strcpy(&string1[0], &string2[0]); と書いても同じこと。この方が 先頭アドレス を指しているのが実感できるが、短い方がスマートなので、上のように書くことが多い。

  7. 15.2文字列処理用ライブラリ関数15.2.1文字列のコピー15.2文字列処理用ライブラリ関数15.2.1文字列のコピー プログラム例15.2.1をスマートに書くと →プログラム例15.2.2 #include<stdio.h> #include<string.h> int main(void) { char a[30], b[30]; char c[] = "University"; strcpy(a, "Japan "); printf("%s\n", a); strcpy(b, c); printf("%s\n", b); strcat(a, b); printf("%s\n", a); return 0; } 文字型配列変数 a, bを宣言 (30 は特に意味なし。 十分なサイズにすれば良い) 文字型配列変数 cの初期値は "University"とする(30 はなくて も適正サイズを確保してくれる) 文字列定数 "Japan" を 配列変数 a にコピー(=代入) 文字列c を文字列bにコピー (=代入)

  8. 15.2文字列処理用ライブラリ関数15.2.1文字列のコピー15.2文字列処理用ライブラリ関数15.2.1文字列のコピー • 関数 strcpyは以下のように定義されている(p.146) char *strcpy(char *ptr1, char *ptr2); コピー(代入)先の 文字型配列の先頭 要素のアドレスを指す ポインタ (=配列名) コピー(代入)元の 文字型配列の先頭 要素のアドレスを指す ポインタ (=配列名) 関数だから戻り値がある。 strcpyの戻り値はptr1(ポインタ) = ptr1[0]のアドレス    = 文字列ptr1

  9. 15.2文字列処理用ライブラリ関数15.2.1文字列の連結15.2文字列処理用ライブラリ関数15.2.1文字列の連結 Catenate means "to connect in a series." 1の後ろに2をつなげる strcat(string1, string2);

  10. 15.2文字列処理用ライブラリ関数15.2.1文字列の連結15.2文字列処理用ライブラリ関数15.2.1文字列の連結 プログラム例 15.2.2 #include<stdio.h> #include<string.h> int main(void) { char a[30], b[30]; char c[] = "University"; strcpy(a, "Japan "); printf("%s\n", a); strcpy(b, c); printf("%s\n", b); strcat(a, b); printf("%s\n", a); return 0; } 1次元文字配列のときは{ }は省略可能 1の後ろに2をつなげる strcat(string1, string2); 文字列 a に文字列b を連結 文字列a はJapan Universityとなる

  11. 15.2文字列処理用ライブラリ関数15.2.1文字列の連結15.2文字列処理用ライブラリ関数15.2.1文字列の連結 • 関数 strcatは以下のように定義されている(p.146) char *strcat(char *ptr1, char *ptr2); 連結先(前半部)の 文字型配列の先頭 要素のアドレスを指す ポインタ (=配列名) 連結する(後半部の) 文字型配列の先頭 要素のアドレスを指す ポインタ (=配列名) 関数だから 戻り値 がある。 strcat の 戻り値はptr1(ポインタ)= ptr1[0] のアドレス    = 文字列 ptr1

  12. 中・上級者向け 15.2文字列処理用ライブラリ関数15.2.1文字列の部分コピー プログラム例 15.2.4 改 #include<stdio.h> #include<string.h> int main(void) { int i; char a[30] = "", b[30] = "Japan"; char c[] = "Kyoto University"; for (i = 0; i < 30; i++) printf("%d ", a[i]); printf("\n"); for (i = 0; i < 30; i++) printf("%d ", b[i]); printf("\n"); printf("%s\n", strcpy(a, c)); printf("%s\n", strncpy(a, b, 5)); return 0; } 文字列を初期化すると、残りの要素に '\0'が入る 文字列a に文字列b の 5文字分をコピー 文字列a は"Japan University"となる

  13. 中・上級者向け 15.2文字列処理用ライブラリ関数15.2.1文字列の部分コピー • 関数strncpyは以下のように定義されている(p.184) char *strncpy(char *ptr1, char *ptr2, int n); コピー(代入)先の 文字型配列の先頭 要素のアドレスを指す ポインタ (=配列名) コピー(代入)元の 文字型配列の先頭 要素のアドレスを指す ポインタ (=配列名) コピー元からコピーする バイト数 文字列a (ptr1が指す)にb (ptr2が指す)の n文字分を上書き 関数だから 戻り値 がある。 strncpy の 戻り値はptr1(ポインタ) ⇔ ptr1[0] のアドレス)

  14. 15.2文字列処理用ライブラリ関数15.2.2その他の関数15.2文字列処理用ライブラリ関数15.2.2その他の関数 strlen(a); 文字列 aの長さを整数値( int型)で返す。 使い方の例: char a[] = "Hello, Goodbye"; int length; length = strlen(a); printf("文章 %s は %d 個の文字から成ります.¥n", a, length); 出力結果: 文章 Hello, Goodbye は 14 個の文字から成ります.

  15. 中・上級者向け 15.2文字列処理用ライブラリ関数15.2.2その他の関数 文字列 aと bを比較し、a < bなら負、 等しいなら 0、 a > bなら正の整数値 ( int型)を返す。 strcmp(a, b); 使い方の例: a - b と同じ符号 char a[] = "Hello", b[] = "Goodbye"; int comp; comp = strcmp(a, b); if (comp < 0) printf("辞書式順序は文字列 %s が前です.¥n", a); if (comp == 0) printf("辞書式順序は等しいです.¥n", a); if (comp > 0) printf("辞書式順序は文字列 %s が前です.¥n", b); 辞書に載っている順 ≒ アルファベット順 出力結果: 辞書式順序は文字列 Goodbye が前です.

  16. a_p 15.2文字列処理用ライブラリ関数15.2.2その他の関数 文字列 aの中の文字 oの位置の ポインタ(アドレス)を返す。 見つからなければ NULLを返す。 strchr(a,'o'); 文字は何でも良い。 探したい文字を指定する 見かけはポインタ 実体は数字のゼロ 使い方の例: "Department of Computer Science" a_p = strchr(a_p,' '); ポインタ a_p から始まる文字列から空白を見つけ、その位置にポインタをセット

  17. 15.2文字列処理用ライブラリ関数15.2.2その他の関数15.2文字列処理用ライブラリ関数15.2.2その他の関数 プログラム例 15.2.5: strlen、strchr の使い方の例 #include<stdio.h> #include<string.h> int main(void) { char a[] = "Department of Computer Science"; char *a_p = a; int length, count; length = strlen(a); printf("文章%s は%d 個の文字から成ります.¥n", a, length); count = 0; while (a_p = strchr(a_p, ' ')) { count++; a_p++; } count++; printf("また、文章に含まれる単語の数は%d 個です.¥n", count); return 0; } 空白の位置を探し、それが 0 でない間は以下を繰り返す 空白の数 + 1 = 単語数

  18. 中・上級者向け 15.2文字列処理用ライブラリ関数15.2.2その他の関数 プログラム例15.2.5 改:strlen、strchr の使い方の例 #include<stdio.h> #include<string.h> int main(void) { char a[] = "Department of Computer Science"; char *a_p = a; int length, count,position; length = strlen(a); printf("文章%s は%d 個の文字から成ります.¥n", a, length); count = 0; while (a_p = strchr(a_p, ' ')) { position = a_p - a + 1; printf("第%d 番目の空白は%d 文字目です.¥n", count + 1, position); count++; a_p++; } count++; printf("また、文章に含まれる単語の数は%d 個です.¥n", count); return 0; } (空白の先頭アドレス) - (文章の先頭アドレス) + 1 で、何文字目かがわかる。

  19. 中・上級者向け 15.2文字列処理用ライブラリ関数15.2.2その他の関数 関数 strlen、strcmpの定義(p.149) int strlen(char *ptr1); 文字列の長さを int型で返す. int strcmp(char *ptr1, char *ptr2); 文字列a (ptr1が指す)とb (ptr2が指す)の辞書式順序を比較し、 a < b なら負、等しいなら0、a > b なら正の整数値(int型)を返す. char *strncpy(char *ptr1, char *ptr2, int n); 文字列a (ptr1が指す)にb (ptr2が指す)の n文字分を上書き. (p.184)

  20. 中・上級者向け 15.2文字列処理用ライブラリ関数15.2.2その他の関数 関数 strchr、strstrの定義(pp.148〜149) char *strchr(char *ptr, char c); 文字列中から文字c を探し、指定文字の位置の ポインタ を返す. なければNULL を返す. char *strstr(char *ptr1, char *ptr2); 文字列1 から文字列2 を探し、文字列2 の位置の ポインタ を返す. なければNULL を返す.

  21. スキルアップタイム 1 • プログラム例 15.2.1〜15.2.4を参考に、以下の動作をするプログラムを作成せよ • 適当な長さの文字型配列変数 a, b, c, d を 宣言 • 日本語の文章1と文章2を適当に決め、おのおの a と b に代入 (例えば、"宇都宮大学 ", "情報工学科 "など) • cに aを、dに bをおのおの コピー • cに bを、dに aをおのおの 連結 • a〜 dをディスプレイに 表示 (表示書式は自分の好みで良い)

  22. PAD ヘッダファイルの取り込み main 文字型配列a~dの宣言 文章1をaに代入 文章2をbに代入 cにaをコピー = dにbをコピー cにbを連結 dにaを連結 a~dを表示

  23. スキルアップタイム 2 • プログラム例 15.2.5 改 を作成・実行する • プログラム例 15.2.4 改 を作成・実行する • 配列 a[30]の初期化(= "")を省略するとどうなるか • 配列 b[30]の 30を省略するとどうなるか • strncpyの第3引数の 5を増減するとどうなるか • 演習問題 15.11) 〜 3) を解く

  24. 本日のパズル次のプログラムは何を出力するか本日のパズル次のプログラムは何を出力するか #include<stdio.h> #define PRINT(format,x) printf("%"#format"\n",x) int integer = 5; char character = '5'; char *string= "5"; main() { PRINT(d,string); PRINT(d,character); PRINT(d,integer); PRINT(s,string); PRINT(c,character); PRINT(c,integer=53); PRINT(d,( '5'>5 )); } 123 45 6 7

More Related