240522

김나정·2024년 5월 22일
0

C

목록 보기
5/8

getchar() putchar()

#include <stdio.h>

int main(void) {
	int ch; //정수형 주의!
	ch = getchar();
	putchar(ch);
	return 0;
}

getch() putch()

#include <stdio.h>

int main(void) {
	int ch; //정수형 주의!
	ch = getch(); //화면에 에코안되고 
	putch(ch); //출력하면서 바로끝남
	return 0;
}

입력버퍼에 남아있는 개행문자

#include <stdio.h>

int main(void) {
	//ch1에는 'a'
	int ch1; 
	ch1 = getchar(); 
	putchar(ch1); 

	getchar(); //개행문자를 없애려고 추가한 갯챠~

	//ch2에는 'b'
	int ch2; 
	ch2 = getchar(); 
	putchar(ch2); 

	return 0;
}

'b'는 입력도 못하고 프로그램 종료
why? 입력 버퍼에 개행문자가 남아있기 때문에 생기는 현상
개행문자를 출력한거임 putchar는 두번실행되고 프로그램이 종료한것뿐임 즉 'b'대신 개행문자가 두번째거가 됨 입력받는 갯챠가 사라짐
how? getchar()를 중간에 한번 호출해서 버퍼를 비워주면됨

->getch를 사용하면 버퍼를 사용하지않으므로 중간에 get을 한번더 안해줘도됨 벗뜨 에코안해줌


Lab 1: 암호 입력받기

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

int main(void) {
	char password[9];
	int i;

	system("cls");
	printf("패스워드를 입력하시오 : ");

	for (i = 0; i < 8; i++) {
		password[i] = getch();
		printf("*");
	}
	password[i] = '\0';
	printf("\n");

	printf("입력된 패스워드는 다음과 같습니다 : ");
	printf("%s\n", password);

	return 0;
}

system("cls")
windows 운영체제에 쓰임.
현재의 터미널 또는 명령 프롬프트 창의 내용을 지움 (Linux : clear, Windows : cls)
cls = clear screen의 약자


#include <stdio.h>

int main(void) {
	int i;
	char ch;
	char solution[] = "meet at midnight";
	char answer[] = "____ __ ________";

	while (1) {
		printf("\n문자열을 입력하시오 : %s \n", answer);
		printf("글자를 추측하시오 : ");
		ch = getch();
		for (i = 0; solution[i] != NULL; i++) {
			if (solution[i] == ch) {
				answer[i] = ch;
			}
		}
		if (strcmp(solution, answer) == 0) {
			printf("정답입니당!!\n");
			break;
		}
	}
	return 0;
}

#include <stdio.h>

int main(void) {
	int i;
	char ch;
	char solution[] = "meet at midnight";
	char answer[] = "____ __ ________";

	for (int cnt = 0; cnt <= 10; cnt++) {
		printf("\n문자열을 입력하시오 : %s \n", answer);
		printf("글자를 추측하시오 (시도 횟수 : %d) : ",cnt);
		if (cnt == 10) {
			printf("최대 시도 횟수(10번)을 초과했습니다.");
		}
		ch = getch();
		for (i = 0; solution[i] != NULL; i++) {
			if (solution[i] == ch) {
				answer[i] = ch;
			}
		}
		if (strcmp(solution, answer) == 0) {
			printf("정답입니당!!\n");
			break;
		}
	}

	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#define SOLUTION "apple"

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

int main(void) {
	char s[100] = SOLUTION;
	char answer[100];
	int len = strlen(s);
	
	for (int i = 0; i < len; i++) {
		int pos1 = rand() % len;
		int pos2 = rand() % len;
		char temp = s[pos1];
		s[pos1] = s[pos2];
		s[pos2] = temp;
	}

	do {
		printf("%s 는 어떤 단어가 스크램블된 것일까요~? : ", s);
		scanf("%s", answer);

	} while (strcmp(SOLUTION, answer) != 0); //strncmp(SOLUTION, answer,5)

	printf("축하합니다.\n");
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#define WORDS 5

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

int main(void) {
	int index;
	char dic[WORDS][2][30] = {
		{"book","책"},
		{"boy","소년"},
		{"computer","컴퓨터"},
		{"language","언어"},
		{"rain","비"}
	};
	char word[30];
	printf("단어를 입력하시오 : ");
	scanf("%s", word);

	for (index = 0; index < WORDS; index++) {
		if (strcmp(word, dic[index][0]) == 0) {
			printf("%s : %s\n", word, dic[index][1]);
			return 0;
		}
	}
	printf("사전에서 해당 단어를 발견할 수 없습니다.\n");
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#define WORDS 5

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

int main(void) {
	int index;
	char dic[WORDS][2][30] = {
		{"book","책"},
		{"boy","소년"},
		{"computer","컴퓨터"},
		{"language","언어"},
		{"rain","비"}
	};
	char word[30];
	printf("단어를 입력하시오 : ");
	scanf("%s", word);

	for (index = 0; index < WORDS; index++) {
		if (strcmp(word, dic[index][0]) == 0) {
			printf("영한 사전 기능을 수행할게요^^\n");
			printf("%s : %s\n", word, dic[index][1]);
			return 0;
		}
	}
	for (index = 0; index < WORDS; index++) {
		if (strcmp(word, dic[index][1]) == 0) {
			printf("한영 사전 기능을 수행할게요^^\n");
			printf("%s : %s\n", word, dic[index][0]);
			return 0;
		}
	}
	printf("사전에서 해당 단어를 발견할 수 없습니다.\n");
	return 0;
}
profile
molbwa

0개의 댓글