II 2008 1030 2008 1106 2008 1113 http

  • Slides: 71
Download presentation
プログラミング論 II 2008年 10月30日 2008年 11月06日 2008年 11月13日 文字列 http: //www. ns. kogakuin. ac.

プログラミング論 II 2008年 10月30日 2008年 11月06日 2008年 11月13日 文字列 http: //www. ns. kogakuin. ac. jp/~ct 13140/Prog. 2008/ H-

解答 0 void main(){ int i; char ch; for(i=0; i<26; i++){ ch = 'A'+i;

解答 0 void main(){ int i; char ch; for(i=0; i<26; i++){ ch = 'A'+i; printf("%d %cn", ch); } } H-15

文字列終端の'�'を忘れると char txt[10]; txt[0]='H'; txt[1]='e'; txt[2]='l'; txt[3]='l'; txt[4]='o'; txt[0] txt[1] txt[2] txt[3] txt[4] txt[5]

文字列終端の''を忘れると char txt[10]; txt[0]='H'; txt[1]='e'; txt[2]='l'; txt[3]='l'; txt[4]='o'; txt[0] txt[1] txt[2] txt[3] txt[4] txt[5] txt[6] txt[7] txt[8] txt[9] 72 101 108 111 3 2 1 0 1 txt[0]~txt[4]に 値が格納される. H-28

文字列終端の'�'を忘れると char txt[10]; 例えば, このような値が 入っていたと する. txt[0] txt[1] txt[2] txt[3] txt[4] txt[5] txt[6]

文字列終端の''を忘れると char txt[10]; 例えば, このような値が 入っていたと する. txt[0] txt[1] txt[2] txt[3] txt[4] txt[5] txt[6] txt[7] txt[8] txt[9] 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 H-31

文字列終端の'�'を忘れると char txt[10]; txt[0]='H'; txt[1]='e'; txt[2]='l'; txt[3]='l'; txt[4]='o'; txt[0]~txt[4]に 値が格納される. txt[0] txt[1] txt[2] txt[3]

文字列終端の''を忘れると char txt[10]; txt[0]='H'; txt[1]='e'; txt[2]='l'; txt[3]='l'; txt[4]='o'; txt[0]~txt[4]に 値が格納される. txt[0] txt[1] txt[2] txt[3] txt[4] txt[5] txt[6] txt[7] txt[8] txt[9] 72 101 108 111 8 7 6 5 4 3 2 1 0 1 H-32

文字列のコピー:strcpy 配列txt[10]に, 'H', 'e', 'l', 'o', '�' の 6個を一括してコピーするには, strcpyを用いる. #include <string. h> void

文字列のコピー:strcpy 配列txt[10]に, 'H', 'e', 'l', 'o', '' の 6個を一括してコピーするには, strcpyを用いる. #include <string. h> void main(){ char txt[10]; strcpy(txt, "Hello"); 900 番地 printf("%sn", txt); 'H' } 901 902 903 904 905 番地 番地 番地 'e' 'l' 'o' '' 一括コピー 実行結果 Hello 100 101 102 103 104 105 番地 番地 番地 'H' 'e' 'l' 'o' '' H-45

文字列のコピー:strcpy #include <string. h> void main(){ char a[10], b[10]; strcpy(a, "Hello"); strcpy(b, a); printf("%sn",

文字列のコピー:strcpy #include <string. h> void main(){ char a[10], b[10]; strcpy(a, "Hello"); strcpy(b, a); printf("%sn", b); } a[0]~a[5]の内容が b[0]~b[5]に 一括コピーされる. 実行結果 Hello H-46

文字列の長さを調べる: strlen char txt[] = "Hello"; int n; n = strlen(txt); printf("%dn", n); 実行結果

文字列の長さを調べる: strlen char txt[] = "Hello"; int n; n = strlen(txt); printf("%dn", n); 実行結果 5 H-47

文字列の結合: strcat char t 0[100] = "Hello"; char t 1[100] = "World!"; strcat(t 0,

文字列の結合: strcat char t 0[100] = "Hello"; char t 1[100] = "World!"; strcat(t 0, t 1); printf("%sn", t 0); 実行結果 Hello. World! H-49

文字列の比較: strcmp 文字列の大小を答える. char t 0[100] = "abc"; char t 1[100] = "def"; int

文字列の比較: strcmp 文字列の大小を答える. char t 0[100] = "abc"; char t 1[100] = "def"; int n; n = strcmp( t 0, t 1); printf("%dn", n); 実行結果 -1 H-51

文字列の比較: strcmp 文字列の大小を答える. char t 0[100] = "ghi"; char t 1[100] = "def"; int

文字列の比較: strcmp 文字列の大小を答える. char t 0[100] = "ghi"; char t 1[100] = "def"; int n; n = strcmp( t 0, t 1); printf("%dn", n); 実行結果 1 H-52

文字列の比較: strcmp 文字列の大小を答える. char t 0[100] = "ABC"; char t 1[100] = "abc"; int

文字列の比較: strcmp 文字列の大小を答える. char t 0[100] = "ABC"; char t 1[100] = "abc"; int n; n = strcmp( t 0, t 1); printf("%dn", n); 実行結果 -1 H-53

文字列を数字に変換: atoi char txt[4] = "123"; int x, y; x = atoi(txt); y =

文字列を数字に変換: atoi char txt[4] = "123"; int x, y; x = atoi(txt); y = x - 111; printf("y = %dn", y); 実行結果 y = 12 H-54

文字列を数字に変換(自作)0/2 char txt[10] = int i, n=0; for(i=0; i<10; if(txt[i] != n = n*10

文字列を数字に変換(自作)0/2 char txt[10] = int i, n=0; for(i=0; i<10; if(txt[i] != n = n*10 + } else { break; } } printf("%dn", "123"; i++){ '' ){ txt[i]-'0'; n); H-55

文字列コピーを自作してみる(い) void string_cpy(char *a, char *b){ int i=0; while ( *(b+i) != '�' ){

文字列コピーを自作してみる(い) void string_cpy(char *a, char *b){ int i=0; while ( *(b+i) != '' ){ *(a+i) = *(b+i); i++; }   *(a+i) = ''; } void main(){ char t 0[100]; 実行結果 char t 1[100] = "abc"; string_cpy(t 0, t 1); abc printf("%sn", t 0); } H-57

文字列コピーを自作してみる(ろ) void string_cpy(char *a, char *b){   while( *b ){ *a = *b; a++; b++;

文字列コピーを自作してみる(ろ) void string_cpy(char *a, char *b){   while( *b ){ *a = *b; a++; b++; }   *a = ''; } void main(){   char t 0[100];   char t 1[100] = "abc";   string_cpy(t 0, t 1);   printf("%sn", t 0); } 実行結果 abc H-66

文字列コピーを自作してみる(は) void string_cpy(char *a, char *b){   while( *b ){ *a++ = *b++; } *a

文字列コピーを自作してみる(は) void string_cpy(char *a, char *b){   while( *b ){ *a++ = *b++; } *a = ''; } void main(){   char t 0[100];   char t 1[100] = "abc";   string_cpy(t 0, t 1);   printf("%sn", t 0); } 実行結果 abc H-67

文字列コピーを自作してみる(に) void string_cpy(char *a, char *b){   do { *a++ = *b++; } while( *(b-1)

文字列コピーを自作してみる(に) void string_cpy(char *a, char *b){   do { *a++ = *b++; } while( *(b-1) ); } void main(){   char t 0[100];   char t 1[100] = "abc";   string_cpy(t 0, t 1);   printf("%sn", t 0); } 実行結果 abc H-68

解答 1 void main(){ int i=0; char txt[100]="hello"; while( txt[i]!='�' ){ i++; } printf("%sの長さは%dですn",

解答 1 void main(){ int i=0; char txt[100]="hello"; while( txt[i]!='' ){ i++; } printf("%sの長さは%dですn", txt, i); } H-70