NekoC

猫でもわかるC言語の個人的なまとめ資料

View on GitHub

08_配列・文字列とポインタ2.md

文字と文字列

サンプルコード


#include <stdio.h>
#include <stdlib.h>

int main(){

	char _char;

	for (_char = '!'; _char <= '~'; _char++){
		printf("\t%3d(0x%2X) -- %c",
			_char, _char, _char);

		if ((_char - 'i' + 1) % 4 == 0){
			printf("\n");
		}
	}
	printf("\n");
		system("pause");
		return 0;
}

出力結果


         33(0x21) -- !   34(0x22) -- "   35(0x23) -- #   36(0x24) -- $
         37(0x25) -- %   38(0x26) -- &   39(0x27) -- '   40(0x28) -- (
         41(0x29) -- )   42(0x2A) -- *   43(0x2B) -- +   44(0x2C) -- ,
         45(0x2D) -- -   46(0x2E) -- .   47(0x2F) -- /   48(0x30) -- 0
         49(0x31) -- 1   50(0x32) -- 2   51(0x33) -- 3   52(0x34) -- 4
         53(0x35) -- 5   54(0x36) -- 6   55(0x37) -- 7   56(0x38) -- 8
         57(0x39) -- 9   58(0x3A) -- :   59(0x3B) -- ;   60(0x3C) -- <
         61(0x3D) -- =   62(0x3E) -- >   63(0x3F) -- ?   64(0x40) -- @
         65(0x41) -- A   66(0x42) -- B   67(0x43) -- C   68(0x44) -- D
         69(0x45) -- E   70(0x46) -- F   71(0x47) -- G   72(0x48) -- H
         73(0x49) -- I   74(0x4A) -- J   75(0x4B) -- K   76(0x4C) -- L
         77(0x4D) -- M   78(0x4E) -- N   79(0x4F) -- O   80(0x50) -- P
         81(0x51) -- Q   82(0x52) -- R   83(0x53) -- S   84(0x54) -- T
         85(0x55) -- U   86(0x56) -- V   87(0x57) -- W   88(0x58) -- X
         89(0x59) -- Y   90(0x5A) -- Z   91(0x5B) -- [   92(0x5C) -- \
         93(0x5D) -- ]   94(0x5E) -- ^   95(0x5F) -- _   96(0x60) -- `
         97(0x61) -- a   98(0x62) -- b   99(0x63) -- c  100(0x64) -- d
        101(0x65) -- e  102(0x66) -- f  103(0x67) -- g  104(0x68) -- h
        105(0x69) -- i  106(0x6A) -- j  107(0x6B) -- k  108(0x6C) -- l
        109(0x6D) -- m  110(0x6E) -- n  111(0x6F) -- o  112(0x70) -- p
        113(0x71) -- q  114(0x72) -- r  115(0x73) -- s  116(0x74) -- t
        117(0x75) -- u  118(0x76) -- v  119(0x77) -- w  120(0x78) -- x
        121(0x79) -- y  122(0x7A) -- z  123(0x7B) -- {  124(0x7C) -- |
        125(0x7D) -- }  126(0x7E) -- ~
続行するには何かキーを押してください . . .

文字列の扱い

文字列型

ポインタに格納した場合

サンプルコード


#include <stdio.h>
#include <stdlib.h>

int main(){

	/* char型のポインタ*/
	char *str;

	str = "ABC";

	printf(" <ポインタによる表示>\n"
		"\t*(str + 0) = '%c'\n"
		"\t*(str + 1) = '%c'\n"
		"\t*(str + 2) = '%c' \n"
		"\t*(str + 3) = '%c' \n\n",
		*(str + 0), *(str + 1), *(str + 2), *(str + 3));

	printf(" <配列による表示>\n"
		"\tstr[0] =  '%c'\n"
		"\tstr[1] =  '%c'\n"
		"\tstr[2] =  '%c'\n"
		"\tstr[3] =  '%c'\n\n",
		str[0], str[1], str[2], str[3]);

	system("pause");

	return 0;
}

出力結果

 <ポインタによる表示>
        *(str + 0) = 'A'
        *(str + 1) = 'B'
        *(str + 2) = 'C'
        *(str + 3) = ' '

 <配列による表示>
        str[0] =  'A'
        str[1] =  'B'
        str[2] =  'C'
        str[3] =  ' '

続行するには何かキーを押してください . . .

文字列の比較

strcmp関数


int strcmp(
		const char string1,
		const char string2
		)

サンプルコード


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){

	char str1[8] = "ABC";
	char str2[8] = "ABC";
	char str3[8] = "DEF";

	/*値の表示*/
	printf("<値>\n");
	printf("\tstr1's value = %s\n", str1);
	printf("\tstr2's value = %s\n", str2);
	printf("\tstr3's value = %s\n", str3);

	/*アドレスの表示*/
	printf("<アドレス>\n");
	printf("\tstr1's address = %p\n", &str1);
	printf("\tstr2's address = %p\n", &str2);
	printf("\tstr3's address = %p\n", &str3);
	printf("※ 同じ文字列でも異なるアドレスに格納される。\n");

	/*文字列比較*/
	// 比較結果格納用の変数
	int cmp1;
	int cmp2;

	cmp1 = strcmp(str1, str2);
	cmp2 = strcmp(str1, str3);

	/*cmp1の判定*/
	printf("\n<cmp1の判定>\n");
	if (cmp1 < 0){
		printf("\t%sは%sより前にあります。\n", str1, str2);
	}
	else if (cmp1 >0){
		printf("\t%sは%sより後ろにあります。\n", str1, str2);
	}
	else if (cmp1 == 0){
		printf("\t%sと%sは同じ文字列です。\n", str1, str2);
	}

	/*cmp2の判定*/
	printf("\n<cmp2の判定>\n");
	if (cmp2 < 0){
		printf("\t%sは%sより前にあります。\n", str1, str3);
	}
	else if (cmp2 >0){
		printf("\t%sは%sより後ろにあります。\n", str1, str3);
	}
	else if (cmp2 == 0){
		printf("\t%sと%sは同じ文字列です。\n", str1, str3);
	}

	system("pause");
	return 0;

}

出力結果


<値>
        str1's value = ABC
        str2's value = ABC
        str3's value = DEF
<アドレス>
        str1's address = 010FFCD0
        str2's address = 010FFCC0
        str3's address = 010FFCB0
※ 同じ文字列でも異なるアドレスに格納される。

<cmp1の判定>
        ABCとABCは同じ文字列です。

<cmp2の判定>
        ABCはDEFより前にあります。
続行するには何かキーを押してください . . .

文字列の長さ

strlen関数


size_t strlen(
		const char *string
		)

サンプルソース


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
	char str[8];
	size_t str_len;

	printf("文字列を入力してください---");
	scanf_s("%s", str,8);

	str_len = strlen(str);
	printf("%sの長さは%dです。\n", str, str_len);

	system("pause");
	return 0;
}

出力結果


文字列を入力してください---abcde
abcdeの長さは5です。
続行するには何かキーを押してください . . .

文字配列

文字列の宣言

文字列の再格納

サンプルコード


#include <stdio.h>
#include <stdlib.h>

int main(){
	char str1[] = "ABC";
	char* str2 = "ABC";

	printf("str1 = %s\n", str1);
	printf("str2 = %s\n", str2);

	str1[0] = 'd';
	str1[1] = 'e';
	str1[2] = 'f';
	/*str[3]はもともと'\0(null)'なので書き換える必要なし*/

	str2 = "def";

	printf("str1 = %s\n", str1);
	printf("str2 = %s\n", str2);

	system("pause");
	return 0;
}

出力結果

str1 = ABC
str2 = ABC
str1 = def
str2 = def
続行するには何かキーを押してください . . .

文字列の入力

サンプルコード


#include <stdio.h>
#include <stdlib.h>

int main(){
	char szStr[32];

	/*************
	下記は不可
	(ポインタはアドレスを格納する変数であるため)
	char *str;
	scanf("%s", str)
	*************/


	printf("szStr = ");
	scanf_s("%s",szStr,32);
	printf("szStr is %s\n", szStr);
	system("pause");
	return 0;
}

出力結果

szStr = Kousuke
szStr is Kousuke
続行するには何かキーを押してください . . .
szStr = Kousuke Konishi
szStr is Kousuke
続行するには何かキーを押してください . . .

gets関数

char *gets(
		char *buffer
		)

サンプルコード


#include <stdio.h>
#include <stdlib.h>

int main(){
	char str[64];

	printf("str = ");
	gets_s(str);
	printf("str is %s\n", str);
	system("pause");
	return 0;
}

出力結果

str = Kousuke Konishi
str is Kousuke Konishi
続行するには何かキーを押してください . . 

サンプルコード2


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
	char str[64], *message = "文字列を入力してください---";
	printf(message);

	while (strcmp(gets_s(str), "")){
		printf("str is %s\n", str);
		printf(message);
	}

	system("pause");
	return 0;
}

出力結果


文字列を入力してください---abc
str is abc
文字列を入力してください---def
str is def
文字列を入力してください---efg
str is efg
文字列を入力してください---
続行するには何かキーを押してください . . .

print(message)について

while(strcmp(gets_s(str1),""))について