본문 바로가기

C언어 콘서트

c언어 콘서트 10장 문자열 LAB (도전문제 포함)

1. 381P 문자열 전처리하기

#define _CRT_SECURE_NO_WARNINGS
#include <conio.h>
#include <stdio.h>
#include <stdlib.h> //rand(), srand()
#include <time.h>  //time()
#include <string.h> //strcmp()문자열비교

int main(void) {
    char input[100] = "";
    char output[100] = "";
    int i, k = 0;

    printf("문자열을 입력하시오: ");
    gets_s(input, sizeof(input));

    for (i = 0; i < strlen(input); i++) {     
        if (('A' <= input[i] && input[i] <= 'Z') || ('a' <= input[i] && input[i] <= 'z')) {
            output[k] = input[i];            
            k++;                             
        }
    }
    
    output[k] = '\0';                       // 마지막
    printf("출력 문자열: %s", output);

    return 0;
}

2. P384 사용자로부터 패스워드 입력받기

#define _CRT_SECURE_NO_WARNINGS
#include <conio.h>
#include <stdio.h>
#include <stdlib.h> //rand(), srand()
#include <time.h>  //time()
#include <string.h> //strcmp()문자열비교

int main(void) {
    char password[10] = "";
    int i;

    printf("패스워드를 입력하시오: ");
    for (i = 0; i < 8; i++) {
        password[i] = _getch();
        printf("*");
    }
    password[i] = '\0';
    printf("\n");

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

    return 0;
}

3. 385P 화살표 키 입력받기

----보드판 입력 정신나갈 것 같아서 생략----

 

4. 396P 단답형 퀴즈 채점

----모르겠다.. 검색해보면 블로그 하나 나오긴하는데, 책에서도 못 본 함수 쓰셔서 생략, 그분 블로그주소는 남김----
 

C언어 80제] C언어 콘서트 CHAPTER 10 p396 도전문제 1 유사문자열도 정답으로 하기

출처 : 반크_독도포스터 참고풀이] //Dev-C++ 5.11로 작업함. #include #include //strlen(), strcmp() #include //toupper() int main() { char key[4][11] = {"C", "C언어", "C LANGUAGE", "C/L"}; char buffer[80]=""; int i; int chk; do { printf("

kmatter.tistory.com

 

5.  399P 행맨 (+++++도전문제 1,2 동시에 포함해서 작성했음. 주석 설명 참조)

#define _CRT_SECURE_NO_WARNINGS
#include <conio.h>
#include <stdio.h>
#include <stdlib.h> //rand(), srand()
#include <time.h>  //time()
#include <string.h> //strcmp()문자열비교

int main()
{
	char sol1[100] = "meet at midnight"; 
	char sol2[100] = "drink all night";
	char sol3[100] = "go home at morning"; 
	char answer[100] = "";	    // 추측하여 맞춘 글자들이 저장될 문자열
	char sol[100] = ""; 		// 난수로 몇번째문제를 낼지 정하면 이 문자열에 복사 후 사용 예정
	char ch; 					// 추측하여 입력한 글자 저장될 변수
	int pick;				    // 문제 솔루션 선택을 위한 변수

 // 난수를 이용하여 sol1, sol2, sol3중 어떤 문제를 낼지 결정하는 부분
	srand(time(NULL));
	pick = rand() % 3;
	if (pick == 0)	strcpy(sol, sol1);
	if (pick == 1)  strcpy(sol, sol2);
	if (pick == 2)  strcpy(sol, sol3);


	// 난수를 통해 랜덤으로 뽑은 문제(sol) 언더바로 변환 후 answer에 이어붙이는 부분
	for (int i = 0; i < strlen(sol); i++)
		if (sol[i] == ' ')
			strcat(answer, " ");
		else
			strcat(answer, "_");

	while (1) { // sol에 들어있는 알파벳들이 무엇일지 맞추는 부분
		printf("문자열을 입력하시오: %s\n", answer);
		printf("글자를 추측하시오: \n");
		ch = _getch();

		for (int i = 0; i < strlen(sol); i++) {
			if (sol[i] == ch)
				answer[i] = ch;
		}
		if (strcmp(sol, answer) == 0)
			break;
	}
	printf("전부 다 맞추셨습니다!\n");
	return 0;
}

6. P401 단어 애나그램 게임

#define _CRT_SECURE_NO_WARNINGS
#include <conio.h>
#include <stdio.h>
#include <stdlib.h> //rand(), srand()
#include <time.h>  //time()
#include <string.h> //strcmp()문자열비교

#define SOL "apple"

int main()
{
	char s[100] = SOL;
	char ans[100] = "";
	int len = strlen(s);
	
	for (int i = 0; i < len; i++) {
		int pos1 = rand() % len;
		int pos2 = rand() % len;
		char tmp = s[pos1];
		s[pos1] = s[pos2];
		s[pos2] = tmp;
	}

	while (1) {
		printf("%s의 원래단어를 맞춰보세요: ", s);
		scanf("%s", ans);
		if (strcmp(ans, SOL) == 0)
			break;
	}
	printf("축하합니다. 정답입니다.");
	return 0;
}

7. 405P 한영사전의 구현 (+++++한글로 단어 입력해도 dic문자열에 있는 내용 출력됨)

#define _CRT_SECURE_NO_WARNINGS
#include <conio.h>
#include <stdio.h>
#include <stdlib.h> //rand(), srand()
#include <time.h>  //time()
#include <string.h> //strcmp()문자열비교

#define WORDS 5

int main()
{
	char search[30] = "";
	char dic[WORDS][2][30] = {
		{ "book", "책" },
		{ "boy", "소년" },
		{ "computer", "컴퓨터" },
		{ "languege", "언어" },
		{ "rain", "비" }
	};

	printf("단어를 입력하시오: ");
	scanf("%s", search);

	for (int i = 0; i < WORDS; i++) {
		if (strcmp(dic[i][0], search) == 0) {
			printf("%s : %s", search, dic[i][1]);
			return 0;
		}
	}
	for (int i = 0; i < WORDS; i++) { // 한글로 단어를 입력해도 그와 일치하는 단어가 dic문자열에 있다면 영어말과 한국말이 같이 출력됨
		if (strcmp(dic[i][1], search) == 0) {
			printf("%s : %s", dic[i][0], search);
			return 0;
		}
	}
	printf("사전에 등재되지 않은 단어입니다.");
	return 0;
}

8. 407P 문자열들을 정렬해보자.

#define _CRT_SECURE_NO_WARNINGS
#include <conio.h>
#include <stdio.h>
#include <stdlib.h> //rand(), srand()
#include <time.h>  //time()
#include <string.h> //strcmp()문자열비교

#define SIZE 6

int main()
{
	char fruits[SIZE][20] = {
		"pineapple",
		"avocado",
		"banana",
		"tomato",
		"pear",
		"apple"
	};

	for (int k = 0; k < SIZE; k++) {
		for (int i = 0; i < SIZE-1; i++) {
			if (strcmp(fruits[i], fruits[i + 1]) > 0) {
				char tmp[20];
				strcpy(tmp, fruits[i]);
				strcpy(fruits[i], fruits[i + 1]);
				strcpy(fruits[i + 1], tmp);
			}
		}
	}
	for (int k = 0; k < SIZE; k++)
		printf("%s \n", fruits[k]);

	return 0;
}