12장 Exercise & Programming
12장 Exercise
1. 3번
2. 2번
3. a) strcat()
b) strcpy()
c) strtok()
d) gets()
e) strlen()
4. a) ‘?’을 “?”로 변경
b) if( strcmp(s, “value”)==0)
c) strcpy(a, “Hello World!”);
5. s1이 의미하는 것은 문자열 상수이다 그러므로 추가 공간이 없기에 s2가 의미하는 문자열을 저장 할 수 없다
char s1[20] = "Hi! ";
char* s2 = "Programmers";
strcat(s1, s2);
6. 첫 번째 문장은 배열 선언, 초기값이 “Hello World!”가 된다
두 번째 문장에서는 메모리에 문자열 상수 저장, 주소가 포인터 p에 대입한다.
7. 4번
8. p가 초기화가 되어 있지 않다. 그러므로 문자열을 저장 할 수 없다
int main(void) {
char p[100];
scanf("%s", p);
}
9. a) 4바이트
b) 4바이트
c) 10바이트
d) 20바이트
10. a) “HIGH”
b) ’D’
c) “HIGH”
11. o
lo
llo
ello
hello
12. mystery()는 s1과 s2가 같으면 0을 받고 다르면 1을 받는다.
mystery(“abc”, “abd”)와 같이 호출하면 1을 받는다
12장 Programming
1.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int len, i;
printf("문자열을 입력하시오: ");
gets_s(str, 100);
len = strlen(str);
printf("역순 문자열: ");
for (i = len - 1; i >= 0; i--) {
printf("%c", str[i]);
}
return 0;
}
2.
#include <stdio.h>
void space_delete(char* str);
void space_delete(char* str) {
int i = 0;
printf("공백 제거 문자열 = ");
while (str[i] != NULL) {
if (str[i] != ' ')
printf("%c", str[i]);
i++;
}
}
void main() {
char str[100];
printf("공백 문자가 있는 문자열을 입력하시오: ");
gets(str);
space_delete(str);
}
3.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
void removeCharacter(char* str, char ch) {
int i, j;
int len = strlen(str);
for (i = j = 0; i < len; i++) {
if (str[i] != ch) {
str[j++] = str[i];
}
}
str[j] = '\0';
}
int main() {
char str[100];
char ch;
printf("문자열을 입력하시오: ");
fgets(str, sizeof(str), stdin);
printf("제거할 문자: ");
scanf("%c", &ch);
removeCharacter(str, ch);
printf("결과 문자열 = %s\n", str);
return 0;
}
4.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int str_chr(char* s, int c) {
int count = 0;
while (*s != '\0') {
if (*s == c) {
count++;
}
s++;
}
return count;
}
int main() {
char str[100];
char ch;
printf("문자열을 입력하시오: ");
fgets(str, sizeof(str), stdin);
printf("문자를 입력하시오: ");
scanf("%c", &ch);
int count = str_chr(str, ch);
printf("\n");
printf("%c의 개수: %d\n", ch, count);
return 0;
}
5.
#include <string.h>
#include <stdio.h>
#define SIZE 100
int str_chr(char* s, int c){
int i;
int count = 0;
for (i = 0; i < strlen(s); i++) {
if (s[i] == c)
count++;
}
return count;
}
int main(void){
char str[SIZE];
char ch;
printf("문자열을 입력하시오: ");
gets(str);
for (ch = 'a'; ch <= 'z'; ch++) {
printf("%c: %d \n", ch, str_chr(str, ch));
}
return 0;
}
6.
#include <stdio.h>
void countCharacters(char* str) {
int alphaCount = 0;
int digitCount = 0;
int specialCount = 0;
while (*str != '\0') {
if ((*str >= 'a' && *str <= 'z') || (*str >= 'A' && *str <= 'Z')) {
alphaCount++;
}
else if (*str >= '0' && *str <= '9') {
digitCount++;
}
else {
specialCount++;
}
str++;
}
printf("\n");
printf("문자열 안의 알파벳 문자의 개수: %d\n", alphaCount);
printf("문자열 안의 숫자의 개수: %d\n", digitCount);
printf("문자열 안의 기타 문자의 개수: %d\n", specialCount);
}
int main() {
char str[100];
printf("문자열을 입력하시오: ");
fgets(str, sizeof(str), stdin);
countCharacters(str);
return 0;
}
7.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int get_response(char* prompt){
char response[100];
printf(prompt);
scanf("%s", response);
if (strcmp(response, "yes") == 0 ||
strcmp(response, "y") == 0 ||
strcmp(response, "YES") == 0 ||
strcmp(response, "Y") == 0)
return 1;
else return 0;
}
int main(void){
int result;
result = get_response("게임을 하시겠습니까: ");
if (result == 1)
printf("긍정적인 답변입니다 \n");
else
printf("부정적인 답변 \n");
return 0;
}
8
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int countWords(char* str) {
char* token;
int count = 0;
token = strtok(str, " ");
while (token != NULL) {
count++;
token = strtok(NULL, " ");
}
return count;
}
int main() {
char str[100];
printf("문자열을 입력하시오: ");
fgets(str, sizeof(str), stdin);
int wordCount = countWords(str);
printf("단어의 수는 %d입니다.\n", wordCount);
return 0;
}
9
#include <stdio.h>
#include <conio.h>
int main(void) {
int i, c;
char password[9];
printf("패스워드를 입력하시오: ");
for (i = 0; i < 8; i++) {
c = _getch();
if (c == '\r') break;
password[i] = c;
printf("*");
}
password[i] = '\0';
printf("\n입력된 패스워드는 %s입니다.", password);
return 0;
}
10.
#include <stdio.h>
#include <string.h>
int countWordOccurrence(char* str, char* word) {
int count = 0;
char* ptr = str;
while ((ptr = strstr(ptr, word)) != NULL) {
count++;
ptr += strlen(word);
}
return count;
}
int main() {
char str[100];
char word[100];
printf("문자열을 입력하시오: ");
fgets(str, sizeof(str), stdin);
printf("단어를 입력하시오: ");
fgets(word, sizeof(word), stdin);
str[strcspn(str, "\n")] = '\0';
word[strcspn(word, "\n")] = '\0';
int wordCount = countWordOccurrence(str, word);
printf("\n");
printf("%s의 개수: %d\n", word, wordCount);
return 0;
}
11.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char s[200] = { 0 };
printf("문자열을 입력하시오:");
gets(s);
if (!isupper(s[0]))
s[0] = toupper(s[0]);
if (s[strlen(s) - 1] != '.') {
s[strlen(s)] = '.';
s[strlen(s) + 1] = NULL;
}
printf("수정된 텍스트:%s\n", s);
return 0;
}
12.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main(void) {
char s[100];
int i, len;
printf("문자열 입력하시오: ");
scanf("%s", s);
len = strlen(s);
for (i = 0; i < len / 2; i++)
if (s[i] != s[len - i - 1]) {
printf("회문이 아닙니다.");
}
printf("회문입니다.");
return 0;
}
13
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100
void reverse_words(char* text) {
int len = strlen(text);
char words[MAX_LENGTH][MAX_LENGTH];
int wordCount = 0;
char* token = strtok(text, " ");
while (token != NULL) {
strcpy(words[wordCount], token);
wordCount++;
token = strtok(NULL, " ");
}
for (int i = wordCount - 1; i >= 0; i--) {
printf("%s ", words[i]);
}
printf("\n");
}
int main() {
char text[MAX_LENGTH];
printf("문자열을 입력하시오: ");
fgets(text, sizeof(text), stdin);
text[strcspn(text, "\n")] = '\0';
reverse_words(text);
return 0;
}
14
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_LENGTH 100
int main() {
char input[MAX_LENGTH];
char last_name[MAX_LENGTH];
char first_name[MAX_LENGTH];
printf("성과 이름을 대문자로 입력하시오: ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0';
sscanf(input, "%s %s", last_name, first_name);
printf("%s %s\n", first_name, last_name);
printf("소문자로 변환: %s %s\n", tolower(first_name[0]), tolower(last_name));
return 0;
}
15.
#include <string.h>
#include <stdio.h>
#define SIZE 100
int get_punc(char* s){
int i;
int count = 0;
for (i = 0; i < strlen(s); i++) {
if (s[i] == ',' || s[i] == '.')
count++;
}
return count;
}
int main(void){
char str[SIZE];
printf("문자열을 입력하시오: ");
gets_s(str, SIZE);
printf("구두점의 개수는 %d입니다.\n", get_punc(str));
return 0;
}
16.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 80
int main() {
char input[MAX_LENGTH];
char find_str[MAX_LENGTH];
char replace_str[MAX_LENGTH];
char result[MAX_LENGTH] = "";
printf("문자열을 입력하시오: ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0';
printf("찾을 문자열: ");
fgets(find_str, sizeof(find_str), stdin);
find_str[strcspn(find_str, "\n")] = '\0';
printf("바꿀 문자열: ");
fgets(replace_str, sizeof(replace_str), stdin);
replace_str[strcspn(replace_str, "\n")] = '\0';
char* token = strtok(input, " ");
while (token != NULL) {
if (strcmp(token, find_str) == 0) {
strcat(result, replace_str);
}
else {
strcat(result, token);
}
strcat(result, " ");
token = strtok(NULL, " ");
}
printf("수정된 문자열: %s\n", result);
return 0;
}
17
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
#define MAX_STRINGS 10
void bubbleSort(char strings[][MAX_LEN], int n) {
int i, j;
char temp[MAX_LEN];
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (strcmp(strings[j], strings[j + 1]) > 0) {
strcpy(temp, strings[j]);
strcpy(strings[j], strings[j + 1]);
strcpy(strings[j + 1], temp);
}
}
}
}
int main(void) {
char strings[MAX_STRINGS][MAX_LEN];
int i, n;
printf("문자열의 개수: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("문자열을 입력하시오: ");
scanf("%s", strings[i]);
}
bubbleSort(strings, n);
printf("\n정렬된 문자열은 다음과 같습니다. \n");
for (i = 0; i < n; i++) {
printf("%s ", strings[i]);
}
return 0;
}
18
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
void main() {
char str[3];
int x, y, result;
printf("연산을 입력하시오: ");
scanf("%s %d %d", &str, &x, &y);
if (strcmp(str, "add") == 0) result = x + y;
else if (strcmp(str, "sub") == 0) result = x - y;
else if (strcmp(str, "mul") == 0) result = x * y;
else if (strcmp(str, "div") == 0) result = x / y;
printf("연산의 결과: %d", result);
}
19
#include <stdio.h>
#include <string.h>
int main(void){
char s[100];
int size, i, k;
printf("텍스트를 입력하시오: ");
gets_s(s, 100);
size = strlen(s);
printf("\n");
for (k = 0; k < size; k++) {
for (i = k; i < (k + size); i++) {
if (i < size)
putchar(s[i]);
else {
putchar(s[i - size]);
}
}
putchar('\n');
}
return 0;
}
20
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100
void xor_encrypt_decrypt(char* data, char* key) {
int dataLen = strlen(data);
int keyLen = strlen(key);
char output[MAX_LENGTH];
for (int i = 0; i < dataLen; i++) {
output[i] = data[i] ^ key[i % keyLen];
}
output[dataLen] = '\0';
strcpy(data, output);
}
int main() {
char text[MAX_LENGTH];
char key[MAX_LENGTH];
printf("텍스트를 입력하시오: ");
fgets(text, sizeof(text), stdin);
text[strcspn(text, "\n")] = '\0';
printf("키를 입력하시오: ");
fgets(key, sizeof(key), stdin);
key[strcspn(key, "\n")] = '\0';
xor_encrypt_decrypt(text, key);
printf("암호화된 문자열: %s\n", text);
xor_encrypt_decrypt(text, key);
printf("복원된 문자열: %s\n", text);
return 0;
}
21
#define _CRT_SECURE_NO_WARNINGS
#define SOL "apple"
#include <stdio.h>
int main(void){
char s[100] = SOL;
char ans[100];
int i, len;
len = strlen(s);
for (i = 0; i < len; i++) {
int pos1 = rand() % len;
int pos2 = rand() % len;
char tmp = s[pos1];
s[pos1] = s[pos2];
s[pos2] = tmp;
}
do {
printf("%s의 원래단어를 맞춰보세요: ", s);
scanf("%s", ans);
} while (strcmp(ans, SOL) != 0);
printf("축하합니다. \n");
return 0;
}
22
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100
void run_length_encoding(char* text) {
int textLen = strlen(text);
char encoded[MAX_LENGTH] = "";
for (int i = 0; i < textLen; i++) {
int count = 1;
while (i + 1 < textLen && text[i] == text[i + 1]) {
count++;
i++;
}
char countStr[10];
sprintf(countStr, "%d", count);
strcat(encoded, countStr);
strncat(encoded, &text[i], 1);
}
strcpy(text, encoded);
}
int main() {
char text[MAX_LENGTH];
printf("문자열을 입력하시오: ");
fgets(text, sizeof(text), stdin);
text[strcspn(text, "\n")] = '\0';
run_length_encoding(text);
printf("%s\n", text);
return 0;
}
13장 Exercise & Programming은 아래 클릭하시면 됩니다
[쉽게 풀어쓴 C언어 Express] 13장 Exercise & Programming
[쉽게 풀어쓴 C언어 Express] 13장 Exercise & Programming
13장 Exercise & Programming 13장 Exercise 1. struct customer { char name[20]; int zip_code; long mileage; }; struct customer c1; 2. a)거짓 b)거짓 c)거짓 d)거짓 e)참 3. 2번 4. 1, 3번 5. 여러 개의 변수가 메모리 공간을 공유하는
gomszone.tistory.com
'프로그래밍 > C언어' 카테고리의 다른 글
[쉽게 풀어쓴 C언어 Express 개정4판] 17장 Exercise & Programming (11) | 2024.02.13 |
---|---|
[쉽게 풀어쓴 C언어 Express 개정4판] 13장 Exercise & Programming (10) | 2024.02.12 |
[쉽게 풀어쓴 C언어 Express 개정4판] 11장 Exercise & Programming (5) | 2024.02.12 |
[쉽게 풀어쓴 C언어 Express 개정4판] 10장 Exercise & Programming (2) | 2024.02.12 |
[쉽게 풀어쓴 C언어 Express 개정4판] 9장 Exercise & Programming (4) | 2024.02.12 |
댓글