본문 바로가기
컴퓨터/코딩

(제3판)C언어 콘서트 CHAPTER 10. Programming 문제 개인풀이

by 혼란한물범 2022. 8. 4.
반응형

개정3판 10장 프로그래밍 문제

개인풀이이기 때문에 출제자가 의도한 정답과 다를 수 있다.

 

 

Visual Studio로 실행해보고 싶은 사람들은 아래 링크에서 해당 문제를 찾아 복사 - 붙여넣기 하자 (다운받지 않아도 된다)

https://mega.nz/file/oDUE3AxC#dPfrridAK6oCn9Iq6QoBuCQjDuK9boga1B90HXpfgmE

 

 

1. 문자열 안에 포함된 문자들의 등장횟수를 계산하는 프로그램을 작성한다. 예를 들어서 문자열 "abc"라면 'a', 'b', 'c' 문자가 한 번씩 등장한다.

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

int main(void) {
char s[100] = { 0 };
int freq[123] = { 0 };

printf("텍스트를 입력하시오: ");
gets_s(s, 99);

for (int i = 0; i < strlen(s); i++)
freq[s[i]]++;
for (int j = 'a'; j <= 'z'; j++)
if (freq[j] != 0)
printf("%c 문자가 %d번 등장하였음!\n", j, freq[j]);


return 0;
}

 

 

2. 사용자로부터 텍스트를 입력받아서 텍스트를 모두 대문자로 출력하는 프로그램을 작성하여 보자. 어떤 라이브러리 함수를 사용하여도 좋다.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100

int main(void) {
char s[SIZE] = { 0 };
char upper[SIZE] = { 0 };

printf("텍스트를 입력하시오: ");
gets_s(s, SIZE - 1);

for (int i = 0; i < strlen(s); i++)
printf("%c", toupper(s[i]));

return 0;
}

 

 

3. 텍스트 안에 포함된 과도한 공백을 없애는 프로그램을 작성하여 보자.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100

int main(void) {
char s[SIZE] = { 0 };
char copy[SIZE] = { 0 };
int count = 0;

printf("텍스트를 입력하시오: ");
gets_s(s, SIZE - 1);

for (int i = 0; i < strlen(s); i++) {
if (s[i] == ' ' && s[(i + 1)] == ' ')
i++;
else if (s[i] == ' ' && s[(i + 1)] != ' ') {
copy[(count)] = s[(i)];
count++;
}
else {
copy[(count)] = s[(i)];
count++;
}
}

copy[(count)] = '\0';

printf("공백이 제거된 텍스트: ");
for (int j = 0; j < strlen(s); j++)
printf("%c", copy[j]);

return 0;
}

 

 

4. 텍스트 안의 모음을 전부 삭제하는 프로그램을 작성하여 본다.

#include <stdio.h>
#define SIZE 100

int main(void) {
char s[SIZE] = { 0 };
char result[SIZE] = { 0 };
int i = 0, j = 0;

printf("텍스트를 입력하시오: ");
gets_s(s, SIZE - 1);

while (s[i] != '\0') {
if (s[i] != 'a' && s[i] != 'i' && s[i] != 'e' && s[i] != 'o' && s[i] != 'u') {
result[j] = s[i];
j++;
}
i++;
}

result[j] = '\0';
printf("최종 텍스트: %s\n", result);

return 0;
}

 

 

5. 사용자로부터 암호를 입력받는다. 사용자의 암호가 해킹에 대하여 강인한지를 검사한다. 만약 암호 안에 대문자, 소문자, 숫자가 모두 들어있으면 강인한 암호로 간주한다. 만약 사용자의 암호가 3가지 종류의 문자를 다 가지고 있지 않으면 프로그램은 보안을 위하여 더 강한 암호를 고려하라고 제안한다.

#include <stdio.h>
#include <string.h>
#define SIZE 100

int main(void) {
char password[SIZE] = { 0 };
int is_lower, is_upper, is_digit;


while (1) {
printf("암호를 생성하시오: ");
gets_s(password, SIZE - 1);

is_lower = is_upper = is_digit = 0;

for (int i = 0; i < strlen(password); i++) {
if (password[i] >= 'a' && password[i] <= 'z')
is_lower = 1;
else if (password[i] >= 'A' && password[i] <= 'Z')
is_upper = 1;
else if (password[i] >= '0' && password[i] <= '9')
is_digit = 1;
}

if (is_lower == 1 && is_upper == 1 && is_digit == 1) {
printf("적정한 암호입니다.");
break;
}
else
printf("숫자, 소문자, 대문자를 섞어서 암호를 다시 만드세요!\n");
}

return 0;
}

 

 

6. 간단한 철자 교정 프로그램을 작성하여 보자. 문장의 끝에 마침표가 존재하는지를 검사한다. 역시 마침표가 없으면 넣어준다. 또한 문자열의 첫 번째 문자가 대문자인지를 검사한다. 만약 대문자가 아니면 대문자로 변환한다. 즉 입력된 문자열이 "pointer is easy"라면 "Pointer is easy."로 변환하여 화면에 출력한다.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100


int main(void) {
char text[SIZE] = { 0 };

printf("텍스트를 입력하시오:");
gets_s(text, SIZE - 1);

text[0] = toupper(text[0]);
if (text[strlen(text) - 1] != '.')
strcat(text, ".");

printf("결과 텍스트 출력:%s", text);

return 0;
}

 

 

7. 문자열의 오른쪽 끝에서 모든 공백 문자들을 제거하는 함수를 작성하라. 함수 원형은 다음과 같다.

#include <stdio.h>
#include <string.h>
#define SIZE 100

void trim_right(char s[]) {
for (int i = (strlen(s) - 1); i > 0; i--) {
if (s[(i - 1)] != ' ') {
s[i] = '\0';
break;
}
}

printf("결과 텍스트 출력: %s(문장끝)\n", s);
}

int main(void) {
char s[SIZE] = { 0 };

printf("텍스트를 입력하시오: ");
gets_s(s, SIZE - 1);

trim_right(s);

return 0;
}

 

 

8. 문자열의 왼쪽 끝에서 모든 공백을 제거하는 함수를 작성하라. 함수 원형은 다음과 같다.

#include <stdio.h>
#include <string.h>
#define SIZE 100

void trim_left(char s[]) {
char copy[SIZE] = { 0 };
int count = 0;

for (int i = 0; i < strlen(s); i++) {
if (s[i] != ' ') {
for (int j = i; j < strlen(s); j++) {
copy[count] = s[j];
count++;
}
break;
}
}

printf("결과 텍스트 출력: (문장처음)%s\n", copy);
}

int main(void) {
char s[SIZE] = { 0 };

printf("텍스트를 입력하시오: ");
gets_s(s, SIZE - 1);

trim_left(s);

return 0;
}

 

 

9. 문자열의 첫 글자를 대문자로 변경하는 함수 capitalize()를 작성한다. toupper() 라이브러리 함수를 사용할 수 있다. toupper() 함수는 소문자를 대문자로 변경하는 라이브러리 함수이다. 함수 원형은 다음과 같다.

#include <stdio.h>
#include <ctype.h>
#define SIZE 100

void capitalize(char s[]) {
s[0] = toupper(s[0]);

printf("결과 텍스트 출력: %s", s);
}

int main(void) {
char s[SIZE] = { 0 };

printf("텍스트를 입력하시오: ");
gets_s(s, SIZE - 1);

capitalize(s);

return 0;
}

 

 

10. 사용자로부터 받은 문자열이 회문(회문은 앞뒤로 동일한 단어이다. 예를 들어서 reviver)인지를 점검하는 프로그램을 작성하라.

#include <stdio.h>
#include <string.h>
#define SIZE 100

void circular(char word[]) {
for (int i = 0; i < strlen(word); i++) {
if (word[i] != word[strlen(word) - 1 - i])
break;
else {
printf("%s는 회문입니다.", word);
return;
}
}
printf("%s는 회문이 아닙니다.", word);
}

int main(void) {
char word[SIZE] = { 0 };

printf("문자열을 입력하시오: ");
gets_s(word, SIZE - 1);

circular(word);

return 0;
}

 

 

11. 화면 캡처 프로그램에서 사용자가 지정한 이름에 ".png"를 붙여서 캡처된 이미지를 디스크에 저장하고자 한다. 파일 이름을 서로 다르게 하려면 어떻게 하는 것이 좋은가? 5개의 파일 이름을 서로 다르게 생성하는 프로그램을 작성해보자. 라이브러리 함수들을 많이 사용해보자.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define SIZE 100

void add_name(char copy[], char png[], char num[]) {
strcat(copy, num);
strcat(copy, png);
printf("%s ", copy);
}

int main(void) {
char s[SIZE] = { 0 };
char copy[SIZE] = { 0 };
char png[5] = { ".png" };
char num[5] = { 0 };

printf("파일 이름의 첫 부분을 입력하시오: ");
gets_s(s, SIZE - 1);

for (int i = 0; i < 5; i++) {
strcpy(copy, s);
sprintf(num, "%d", i);
add_name(copy, png, num);
}

return 0;
}

 

 

12. 1960년대에 채팅봇의 원조라고 하는 ELIZA가 등장하였다. ELIZA는 상당한 기능을 가지고 있어서 상담하는 사람들이 진짜 인간이라고 믿었다고 한다. 우리는 아주 간단한 채팅 봇을 만들어보자. 사용자가 질문을 하면 그럴싸한 답변을 한다. 사용자의 이름도 물어봐서 저장해두었다가 사용해보자. 사용자가 한 이야기에 맞장구를 치는 것도 하나의 채팅 기법이다.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 1000

int main(void) {
char s[SIZE] = { 0 };
char rep[SIZE] = { 0 };
char name[30] = { 0 };
char name_rem[30] = { 0 };
char hi[3] = { "hi" };
char hello[6] = { "hello" };
char name_question[10] = { "your name" };
char name_reply[] = { ">좋은 이름이네요, " };
char name_diff[] = { ">처음에는 그 이름이 아니지 않았나요, " };
int name_count = 0;

srand(time(NULL));

while (1) {
printf(">");
gets_s(s, SIZE - 1);

if (strlen(s) > 100)
printf(">말이 너무 길군요. 조금 요약해주시겠어요?\n");
else if (strcmp(hi, s) == 0 || strcmp(hello, s) == 0)
printf(">반가워요\n");
else if (strstr(s, name_question) != NULL)
printf(">제 이름 말인가요? ELIZA라고 해요.\n");
else if (rand() % 10 >= 8) {
printf(">잠깐, 당신의 이름이 뭔지 물어봐도 될까요?\n");
printf(">");
gets_s(name, 29);
name_count++;
if (name_count >= 2 && strcmp(name_rem, name) != 0) {
printf("%s", name_diff);
printf("?\n");
}
else if (name_count >= 2 && strcmp(name_rem, name) == 0)
printf(">이런, 아까 들었는데 그만 깜빡했네요.\n");
else {
strcpy(name_rem, name);
strcat(name_diff, name_rem);
strcat(name_reply, name);
printf("%s\n", name_reply);
}
}
else if (rand() % 10 < 8 && rand() % 10 >= 5)
printf(">계속하세요. 나는 듣고 있어요\n");
else if (rand() % 10 < 5)
printf(">흥미롭네요\n");
}

return 0;
}

반응형

댓글