1 / 10

C言語応用

C言語応用. 文字列その2. 文字列の操作. 文字列の複写. 配列 s に中身を” ABCD” にしたければ、. s[0] = ’A’; s[1] = ’B’; s[2] = ’C’; s[3] = ’D’; s[4] = ’’;. といった具合に、配列の要素に1つ1つ値を代入することで可能である。. http://www.center.nitech.ac.jp/~sfukui/ouyou/list12-1.c. 文字列の操作. 標準ライブラリ strcpy. strcpy. ヘッダ.

efrem
Download Presentation

C言語応用

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. C言語応用 文字列その2

  2. 文字列の操作 • 文字列の複写 配列 s に中身を”ABCD”にしたければ、 s[0] = ’A’; s[1] = ’B’; s[2] = ’C’; s[3] = ’D’; s[4] = ’\0’; といった具合に、配列の要素に1つ1つ値を代入することで可能である。 http://www.center.nitech.ac.jp/~sfukui/ouyou/list12-1.c

  3. 文字列の操作 • 標準ライブラリ strcpy strcpy ヘッダ #include<string.h> 形式 char *strcpy(char *s1, const char *s2); 解説 s1 の指す領域に、s2の指す文字列を’\0’を含めて複写する。 戻り値 s1の値を返す。

  4. 文字列の操作 • 文字列の連結 char *strconcat(char s[] , const char t[]) { int i,j; for(i = 0;s[i] != ’\0’;i++); for(j = 0;(s[i++] = t[j++]) != ’\0’; ); return(s); } http://www.center.nitech.ac.jp/~sfukui/ouyou/list12-2.c

  5. 文字列の操作 • 標準ライブラリ strcat strcat ヘッダ #include<string.h> 形式 char *strcat(char *s1, const char *s2); 解説 s1 の指す文字列の末尾に、s2の指す文字列を連結する。 戻り値 s1の値を返す。

  6. 文字列の操作 • 標準ライブラリ atoi atoi ヘッダ #include<stdlib.h> 形式 int atoi(const char *str); 解説 str の指す文字列を整数に変換する。 変換された整数値。変換が失敗したときの戻り値は定義されない。 戻り値

  7. 文字列とポインタ • ポインタと文字列 char str1[] = ”ABC”; char *str2 = ”ABC”; どちらでもよい。 配列 ポインタ ’A’ ”ABC” (100) 番地 str2 str1[0] ’B’ str1[1] ’C’ str1[2] 100 ’A’ str2[0] ’\0’ str1[3] ’B’ str2[1] ’C’ str2[2] ’\0’ str2[3] http://www.center.nitech.ac.jp/~sfukui/ouyou/list12-3.c

  8. 文字列とポインタ • 文字列の複写 char *strcopy(char *s , const char *t) { char *p = s; while ((*s = *t) != ’\0’){ s++; t++; } return(p); } http://www.center.nitech.ac.jp/~sfukui/ouyou/list12-4.c

  9. ’N’ s[0][0] ’A’ s[0][1] ’\0’ s[0] s[0][2] ’\0’ s[0][3] ’\0’ s[0][4] ’D’ s[1][0] ’O’ s[1][1] ’H’ s[1] s[1][2] ’C’ s[1][3] ’\0’ s[1][4] 文字列の配列 • 2元配列による実現 ”NA”,”DOHC”の2つの文字列の配列を、「配列文字列」の配列として実現する。 配列を要素とする配列は、2元配列である。 http://www.center.nitech.ac.jp/~sfukui/ouyou/list12-5.c

  10. 演習 • 曜日に対応する文字列を表示する関数 void putweekstr(int n){ ・・・ } を作成せよ。たとえばputweekstr(0)は、文字列 ”Sunday”を表示する。 • 曜日に対応する文字列へのポインタをかえす関数 char *weekstr(int n){ ・・・ } を作成せよ。たとえばweekstr(0)は、文字列 ”Sunday”へのポインタを返すようにする。 • 2つの「charへのポインタ」の中身を入れ換える関数 void swapptr(char *x, char *y){ ・・・ } を作成せよ。

More Related