NekoC

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

View on GitHub

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

文字列の加工

strcpy関数


char *strcpy(
char *strDestination,
const char *strSource
)

strcat関数


char *strcat(
char *strDestination;
const char *strSource;
)

サンプルコード


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

int main(){
	char str[] = "ABC";
	strcpy_s(str, "def");
	printf("str is %s\n", str);

	char name[64];
	char honorific[] = "様";

	printf("What's your name? -> ");
	gets_s(name);
	strcat_s(name, honorific);
	printf("Welcome %s!\n", name);

	system("pause");
	return 0;

}

出力結果


str is def
What's your name? -> Kosuke Konishi
Welcome Kosuke Konishi様!
続行するには何かキーを押してください . . .

書式にのっとって加工を行う

sprintf関数


int sprintf(
		char *buffer,
		const char *format [,
		argument] ...
		)

サンプルコード


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

int main(){
	char buffer[128], name[32];
	int age;
	double height, weight;
	int endFlg=1;

	do{
		printf("氏名---");
		gets_s(name);
		if (!strcmp(name, "")){
			printf("氏名が入力されていません。\n");
		}
	} while (!strcmp(name, ""));

	do{
		printf("年齢---");
		scanf_s("%d", &age);
		if (age < 0 || age > 150){
			printf("年齢が不適切です。\n");
		}
	} while (age < 0 || age > 150);

	do{
		printf("身長(cm)---");
		scanf_s("%lf", &height);
		if (height < 0.0 || height > 250.0){
			printf("身長の値が不正です。\n");
		}
	} while (height < 0.0 || height > 250.0);

	do{
		printf("体重(kg)---");
		scanf_s("%lf", &weight);
		if (weight < 0.0 || weight > 200.0){
			printf("体重の値が不正です。\n");
		}
	} while (weight < 0.0 || weight > 200.0);

	sprintf_s(buffer,
		"\n%sさん(年齢は%d歳)のプロフィール\n"
		"身長は%5.1fcm, 体重 %5.1fkgで、BMIは%4.1fです\n",
		name, age, weight, height, weight * 10000.0 / (height * height)
		);
	printf(buffer);

	system("pause");
	return 0;

}

出力結果


氏名---Kosuke Konishi
年齢---27
身長(cm)---175
体重(kg)---62

Kosuke Konishiさん(年齢は27歳)のプロフィール
身長は 62.0cm, 体重 175.0kgで、BMIは20.2です
続行するには何かキーを押してください . . .

文字列ポインタを配列にする

サンプルコード


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

int main(){
	char *str[6] = {
		"abc",
		"de",
		"fghi",
		"jklmnop",
		"qrs",
		"tuvwxyz"
	};

	int count;
	for (count = 0; count < 6; count++){
		printf("str[%d]のアドレス = %p\n", count, &str[count]);
		printf("str[%d][0]の値 = %c\n", count, str[count][0]);
	}
	system("pause");
	return 0;
}

出力結果


str[0]のアドレス = 003EFDAC
str[0][0]の値 = a
str[1]のアドレス = 003EFDB0
str[1][0]の値 = d
str[2]のアドレス = 003EFDB4
str[2][0]の値 = f
str[3]のアドレス = 003EFDB8
str[3][0]の値 = j
str[4]のアドレス = 003EFDBC
str[4][0]の値 = q
str[5]のアドレス = 003EFDC0
str[5][0]の値 = t
続行するには何かキーを押してください . . .

コマンドライン引数

サンプルコード


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

int main(int argc, char *argv[]){
	int argCount;

	if (argc == 1){
		printf("コマンドライン引数がありません。\n");
		system("pause");
		return -1;
	}
	for (argCount = 0; argCount < argc; argCount++){
		printf("argv[%d] is %s\n", argCount, argv[argCount]);
	}
	system("pause");
	return 0;
}

出力結果


argv[0] is C:\Users\nl_konishi\Documents\GitHub\Study\02_技術\猫でもわかるC言語プログラミング\CanAlsoCat\Debug\commandlineArgs.exe
argv[1] is test1
argv[2] is test2
続行するには何かキーを押してください . . .

関数に配列を渡す

サンプルコード


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

int showName(char **);

int main(){
	char *name[] = {
		"John Nash",
		"Tyler Durden",
		"Marty Macfly",
		"Rasty"
	};

	if (!showName(name) == 0){
		printf("エラー!");
		system("pause");
		return -1;
	}
	system("pause");
	return 0;
}

int showName(char **p){
	int count;
	for (count = 0; count < 4; count++){
		printf("%s\n", p[count]);
	}

	return 0;
}

出力結果


John Nash
Tyler Durden
Marty Macfly
Rasty
続行するには何かキーを押してください . . .