初心者のためのC言語入門講座第4単元
2023年10月25日配列変数について
●ソース1
#include <stdio.h>
void main(){
char a[5];
int b;
b=0;
a[0]='A';
a[1]='S';
a[2]='C';
a[3]='I';
a[4]='I';
while(b<=4){ printf("%c",a[b]); b++; }
}
while文の復習
文末記号について
●ソース2
#include <stdio.h>
void main(){
char a[]="abcdefg"; //いちばん最後の番地に 文末記号'\0'が格納
int b=0;
while(a[b]!='\0'){ printf("%c",a[b]); b++; }
printf("\n");
printf("End");
}
文字列の比較について
●ソース3
#include <stdio.h>
#include <string.h> //文字列を操作する機能を読込
void main(){
char a[]="abcdefg";
char b[]="abcdefg";
int hantei;
//==は数値の比較に利用し、文字列の比較ができないので以下のように比較する
hantei=strcmp(a,b); //等しいかチェック 0 等しい場合 でない場合は-1もしくは1が格納
if(hantei==0){ printf("同じ"); }else{ printf("違う"); }
}
文字列のコピーについて
●ソース4
#include <stdio.h>
#include <string.h>
void main(){
char a[]="source";
char b[100];
strcpy(b,a);
printf("%s",b);
}
文字数を数える
●ソース5
#include <stdio.h>
#include <string.h>
void main(){
char a[]="length of the string";
int b;
b=strlen(a);
printf("%d文字です",b);
}