본문 바로가기
프로그램/C언어 1000제

C언어 83제] C언어 콘서트 CHAPTER 10 p400 도전문제 1 프로그램 업그래이드

by 건티 2022. 1. 1.
728x90

출처 : 반크_독도포스터

 

참고풀이]

//Dev-C++ 5.11로 작업함.
#include <stdio.h>
#include <conio.h> //getch()
#include <string.h> //strcmp()
#include <stdlib.h> //srand(), rand()
#include <time.h> //time()

int main(void) {
   char solution[3][100] ={
    "meet at midnight",
    "new year's greeting",
    "happy birthday"}; // 정답이 들어 있는 문자 배열
   char answer[3][100] ={
    "____ __ ________",
    "___ ______ ________",
    "_____ ________"}; // 현재까지 사용자가 맞춘 문자열
   char hint[3][100]={
    "한밤중에 만나다",
    "새해인사",
    "생일축하"};
   char ch;
   int i;
   int key;

   //실행할 때마다 무작위 수를 발생하도록 초기값을 설정. 
   srand(time(NULL));

   key=rand()%3;
   while (1) {
      printf("힌트 : %s\n",hint[key]);
      printf("문자열을 입력하시오: %s \n", answer[key]);
      printf("글자를 추측하시오: ");
      ch = getch();

      // 사용자가 입력한 문자를 answer[key][]에 넣어서  정답을 비교한다.
      for (i = 0; solution[key][i] != '\0'; i++) {
         // 사용자가 맞추었으면 글자를 보이게 한다.
         if (solution[key][i] == ch) answer[key][i] = ch;
      }
      // 정답과 일치하는지를 검사
      if (strcmp(solution[key], answer[key]) == 0) break; 
      printf("\n\n");
   }

   printf("\n\n수고하셨습니다.\n");
   return 0;
}

 

참고풀이 결과]

 

 

 

대한민국의 아름다운 영토, 독도의 봄

 

반응형

댓글