II 2010 10 http www ns kogakuin ac

  • Slides: 71
Download presentation
プログラミング論 II 2010年 10月 文字列 http: //www. ns. kogakuin. ac. jp/~ct 13140/Prog. 2010/ D-

プログラミング論 II 2010年 10月 文字列 http: //www. ns. kogakuin. ac. jp/~ct 13140/Prog. 2010/ D-

解答 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); } } D-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]に 値が格納される. D-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 D-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 D-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 106 107 108 109 番地 番地 番地 'H' 'e' 'l' 'o' '' D-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 D-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 #include <string. h> が必要です. D-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! #include <string. h> が必要です. D-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 #include <string. h> が必要です. D-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 #include <string. h> が必要です. D-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 #include <string. h> が必要です. D-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 #include <stdlib. h> が必要です. D-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); D-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); } D-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 D-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 D-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 D-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); } D-70