본문 바로가기
IT/프로그래밍

쉽게 풀어 쓴 C언어 Express 12장 프로그래밍 문제 해설

by nutrient 2020. 12. 10.
728x90
728x170

쉽게 풀어 쓴 C언어 Express 12장 프로그래밍 문제 해설

쉽게 풀어 쓴 C언어 Express 12장 프로그래밍 문제 해설

 

1.

#include <stdio.h> 

int main(void) { 

char ch; 

printf("문자를 입력하시오: "); 

scanf("%c", &ch); 

printf("아스키 코드값=%d\n", ch); 

return 0; 

} 



2.

#include <string.h> 

#include <stdio.h> 

#define SIZE 100 

void delete_space(char s[]) { 

char tmp[SIZE]; 

int i, k=0; 

for (i=0;i<(int)strlen(s);i++) { 

if( s[i] != ' ' ) 

tmp[k++] = s[i]; 

} 

tmp[k]=0; 

strcpy(s, tmp); 

} 

int main(void) { 

char str[SIZE]; 

printf("공백 문자가 있는 문자열을 입력하시오: "); 

gets(str); 

delete_space(str); 

printf("%s", str); 

return 0; 

} 



3.

#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); 

printf("개수를 셀 문자를 입력하시오: "); 

ch = getchar(); 

printf("%c의 개수: %d", ch, str_chr(str, ch)); 

return 0; 

} 



4.

#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; 

} 



5.

#include <stdio.h> 

#include <string.h> 

#include <ctype.h> 

int main(void) { 

char c; 

while(1) { 

printf("문자를 입력하시오: "); 

c=getchar(); 

if( islower(c) ) 

putchar(toupper(c)); 

if( isupper(c) ) 

putchar(tolower(c)); 

if( !isalpha(c) ) 

printf("경고 "); 

if( c=='.') break; 

fflush(stdin); 

// 줄바꿈 문자 제거 

} 

return 0; 

} 



6.

#include <string.h> 

#include <stdio.h> 

#define SIZE 100 

void str_upper(char *s) { 

int i; 

int count=0; 

for (i=0;i<strlen(s);i++) { 

if( s[i] >='a' && s[i] <= 'z' ) 

s[i] = s[i]-'a'+'A'; 

} 

} 

int main(void) { 

char str[SIZE]; 

printf("문자열을 입력하시오: "); 

gets(str); 

str_upper(str); 

printf("변환된 문자열: %s\n", str); 

return 0; 

} 



7.

#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("긍적적인 답변); 

else 

printf("부정적인 답변); 

return 0; 

} 



8.

#include <string.h> 

#include <stdio.h> 

char s[] = "Man is immortal, because he has a soul"; 

char seps[] = " ,\t\n"; 

char *token; 

int main( void ) { 

int count=0; 

token = strtok( s, seps ); 

while( token != NULL ) { 

count++; 

token = strtok( NULL, seps ); 

} 

printf("단어의 수는 %d입니다.\n", count); 

return 0; 

} 


9.

#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; 

} 



10.

#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; 

} 



11. 포인터 배열도 이용하여야 한다. 

#include <stdio.h> 

#include <string.h> 

int main(void) { 

char s[100]; 

char *ptr[100]; 

int i=0; 

int j; 

puts("문자열을 입력하시오: "); 

gets(s); 

ptr[0]=strtok(s," "); 

while(ptr[i] != NULL) { 

ptr[i+1]=strtok(NULL," "); 

i++; 

} 

for (j=i-1;j>=0;j--) { 

printf("출력 문자열: %s ",ptr[j]); 

} 

return 0; 

} 



12.

#include <string.h> 

#include <stdio.h> 

#define SIZE 100 

int main(void) { 

char seps[] = ", "; 

char s[SIZE]; 

char *token, *name, *fname; 

int i; 

printf("성과 이름을 대문자로 입력하시오: "); 

gets(s); 

token = strtok( s, seps ); 

// 문자열에서 첫번째 토큰을 얻는다. fname = token; 

token = strtok( NULL, seps ); 

// 다음 토큰을 얻는다. name = token; 

for (i=0;i<strlen(name);i++) 

name[i] = tolower(name[i]); 

for (i=0;i<strlen(fname);i++) 

fname[i] = tolower(fname[i]); 

printf("%s, %s\n", name, fname); 

return 0; 

} 


13.

#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(str); 

printf("구두점의 개수는 %d입니다.\n", get_punc(str)); 

return 0; 

} 



14.

#include <stdio.h> 

#include <string.h> 

#include <conio.h> 

int main(void) { 

char seps[] = " "; 

char s[200], find[100], replace[100], target[200]=""; 

char *token; 

printf("문자열을 입력하시오: "); 

gets(s); 

printf("찾을 문자열: "); 

gets(find); 

printf("바꿀 문자열: "); 

gets(replace); 

token = strtok( s, seps ); 

// 문자열에서 첫번째 토큰을 얻는다. while( token != NULL ) { 

if( strcmp(token, find)==0 ) 

strcat(target, replace); else 

strcat(target, token); 

token = strtok( NULL, seps ); 

// 다음 토큰을 얻는다. strcat(target, " "); 

} 

printf("결과: %s", target); 

return 0; 

} 



15.

#include <stdio.h> 

#include <string.h> 

int main(void) { 

char s[100]= { 

0 

} 

; 

char op[100]; 

int x,y; 

char *token; 

printf("연산을 입력하시오:"); 

gets(s); 

token = strtok( s, " " ); 

// 문자열에서 첫번째 토큰을 얻는다. strcpy(op, token); 

token = strtok( NULL, " " ); 

// 다음 토큰을 얻는다. x =atoi(token); 

token = strtok( NULL, " " ); 

// 다음 토큰을 얻는다. y =atoi(token); 

if( strcmp(op, "add")==0 ) { 

printf("연산의 결과: %d", x+y); 

} else if( strcmp(op, "sub")==0 ) { 

printf("연산의 결과: %d", x-y); 

} else if( strcmp(op, "mul")==0 ) { 

printf("연산의 결과: %d", x*y); 

} else if( strcmp(op, "div")==0 ) { 

printf("연산의 결과: %d", x/y); 

} else { 

} 

return 0; 

} 



16.

#include <stdio.h> 

#include <string.h> 

int main(void) { 

char s[100]; 

int size, i, k; 

puts("광고하고 싶은 텍스트를 입력하시오: "); 

gets(s); 

size = strlen(s); 

puts("======================"); 

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; 

}
728x90
그리드형

댓글