C - C언어 for Beginner 개정 4판 연습문제 풀이
2022. 6. 27. 19:45ㆍC
6장 반복문의 기초, for문
9번 입력된 문자열을 반대로 출력하면서 동시에 숫자는 #으로 변경하는 코드 만들기
#include <stdio.h>
#include <string.h>
int main(void){
char str[100];
int str_cnt;
int i;
printf("영문자 및 숫자를 입력: ");
scanf("%s", str);
printf("\n");
printf("입력한 문자열 ==> %s\n", str);
printf("변환된 문자열 => ");
str_cnt = strlen(str);
for(i=str_cnt;i>-1;i--){
if(str[i]>47&&str[i]<58){ //숫자 0~9의 아스키코드는 48~57
str[i]='#';
}
printf("%c", str[i]);
}
return 0;
}
10번 1~100중에서 홀수의 합과 짝수의 합을 각각 구하는 프로그램
#include <stdio.h>
#include <string.h>
int main(void){
int i;
int odd_hap; //홀수 합계
int even_hap; //짝수 합계
for (i = 1; i <= 100; i++)
{
if(i%2==0){
even_hap += i;
}
else odd_hap += i;
}
printf("홀수의 합 >> %d\n",odd_hap);
printf("짝수의 합 >> %d", even_hap);
return 0;
}
1~1000 중에서 3의 배수 혹은 7의 배수의 합계를 구하는 코드
#include <stdio.h>
#include <string.h>
int main(void){
int i;
int hap = 0;
for (i = 1; i <= 1000; i++)
{
if(i%3==0 || i%7==0) hap+=i;
}
printf("3의 배수이거나 7의 배수의 합>>%d", hap);
return 0;
}
7장 while문과 흐름 제어
입력된 문자열의 종류 구분 (대문자, 소문자, 숫자 카운트)
#include <stdio.h>
#include <string.h>
int main(void){
char str[100];
char ch;
int upper_cnt=0, lower_cnt=0, digit_cnt=0, i=0;
printf("문자열을 입력");
scanf("%s", str);
do{
ch = str[i];
if(ch>='A'&&ch<='Z') upper_cnt++;
if(ch>='a'&&ch<='z') lower_cnt++;
if(ch>='0'&&ch<='9') digit_cnt++;
i++;
} while(ch!= '\0');
printf("대문자 %d개 소문자 %d개 숫자 %d개\n", upper_cnt, lower_cnt, digit_cnt);
return 0;
}
입력된 숫자만큼 별표 출력
#include <stdio.h>
#include <string.h>
int main(void){
char str[100];
char ch;
int i,k;
int star;
printf("숫자 입력: \n");
scanf("%s", str);
i=0;
ch = str[i];
while(ch != '\0'){
star = (int)ch-48;
for(k=0;k<star;k++){
printf("*");
}
printf("\n");
i++;
ch = str[i];
}
return 0;
}
4번 100~200까지의 합계를 내는 코드
#include <stdio.h>
int main(void){
int hap = 0;
int i=100;
while(i<=200){
hap+=i;
i++;
}
printf("100~200까지의 합 %d \n", hap);
return 0;
}
7번 입력한 두 수 사이의 홀수를 구하는 코드
#include <stdio.h>
int main(void){
int start, end;
scanf("%d %d", &start, &end);
if(start<end){
for(int i = start; i<=end;i++){
if(i%2!=0) printf("%d ", i);
}
}
else {
for(int i=end; i<=start; i++){
if(i%2!=0) printf("%d ", i);
}
}
return 0;
}
8번 입력한 숫자의 2배만큼 하트가 출력되는 코드
#include <stdio.h>
int main(void){
char str[10];
char ch;
printf("정수를 입력하세요>>");
scanf("%s", str);
int i=0;
ch = str[i];
int love;
while(ch!='\0'){
love = (int)str[i]-48;
for(int j=0;j<love;j++){
printf("\u2665");
}
printf("\n");
i++;
ch = str[i];
}
return 0;
}
8장 배열
대소문자 변환 프로그램
#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>
#include <string.h>
void main(void){
char in[50], out[50];
int i, len;
int diff = 'a'-'A';
printf("문자 입력>>");
gets(in);
len = strlen(in);
for(i=0;i<len;i++){
if(('A'<=in[i])&&(in[i]<='Z')){
out[i] = in[i]+diff;
}
else if(('a'<=in[i])&&(in[i]<='z')){
out[i] = in[i]-diff;
}
else out[i] = in[i];
}
out[i] = '\0';
printf("변환된 문자 >> %s \n", out);
}
문자열 내 특정 문자의 변환
#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>
#include <string.h>
void main(void){
char str[100];
char ch1, ch2;
int i;
printf("글자입력 : ");
gets(str);
printf("기존 문자와 새로운 문자 ");
scanf("%c %c", &ch1, &ch2);
for (i = 0; i < strlen(str); i++)
{
if(str[i] == ch1) str[i] = ch2;
}
printf("변환된 결과 >> %s\n", str);
}
8번 코드 실행 결과
#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>
#include <string.h>
void main(void){
int ary[2][3];
int i,k;
int num = 10;
for (i = 0; i < 2; i++)
{
for (k = 0; k < 3; k++)
{
ary[i][k] = --num;
}
}
for (i = 0; i < 2; i++)
{
for (k = 0; k < 3; k++)
{
printf("%3d", ary[i][k]);
}
printf("\n");
}
}
//결과
9 8 7
6 5 4
9장 배열과 포인터
포인터를 이용하여 문자열을 반대로 출력
#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>
#include <string.h>
void main(void){
char ss[100];
int count, i;
char *p;
printf("문자열을 입력하세요");
scanf("%s", ss);
count = strlen(ss);
p = ss;
printf("\n문자열을 거꾸로 출력 >>");
for (i = 0; i < count; i++)
{
printf("%c", *(p+count-(i+1)));
}
}
포인터를 이용한 두 값의 교환(Swap)
#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>
#include <string.h>
void main(void){
int a, b, temp;
int *p1, *p2;
printf("a, b>>");
scanf("%d %d", &a, &b);
printf("a,b 값은 %d %d \n", a, b);
p1 = &a;
p2 = &b;
temp = *p1;
*p1 = *p2;
*p2 = temp;
printf("a,b의 값은 %d %d \n", a, b);
}
포인터를 이용한 배열의 정렬
#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>
#include <string.h>
void main(void){
int s[10] = {1,0,3,2,5,4,7,6,9,8};
int tmp, i, k;
int *p;
p = s;
printf("정렬전 배열");
for(i=0;i<10;i++){
printf("%d ", *(p+i));
}
printf("\n");
for(i=0;i<9;i++){
for ( k = i+1; k<10; k++)
{
if(*(p+i)>*(p+k)){
tmp = *(p+i);
*(p+i) = *(p+k);
*(p+k) = tmp;
}
}
}
printf("정렬 후 배열");
for(i=0;i<10;i++){
printf("%d ", *(p+i));
}
printf("\n");
}
8번 글자가 거꾸로 출력되고 영문 대문자&&소문자가 상호 변환되는 실행결과가 나오는 코드(단 ary가 아닌 p를 사용)
#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>
#include <string.h>
void main(void){
char ary[25] = "IT 1234 @$! CookBook";
char *p;
int i;
p = ary;
int diff = 'a'-'A';
for(i = sizeof(ary)-2; i>=0;i--){
if(('A'<=*(p+i))&&('Z'>=*(p+i))){ //대문자면
*(p+i) = *(p+i)+diff;
}
else if(('a'<=*(p+i))&&('z'>=*(p+i))){ //소문자면
*(p+i) = *(p+i)-diff;
}
printf("%c",*(p+i));
}
}
10장 함수
8번 upper_lower 함수
#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int diff = 'a'-'A';
char* upper_lower(char a[]){
for(int i=0;i<sizeof(a);i++){
if(a[i]>='A'&&a[i]<='Z'){
a[i] = a[i]+diff;
}
else if(a[i]>='a'&&a[i]<='z'){
a[i] = a[i]-diff;
}
}
return a;
}
void main(){
char inStr[100], outStr[100];
printf("문자열 입력>>");
scanf("%s", inStr);
strcpy(outStr, upper_lower(inStr));
printf("대소문자 변환 결과 ==> %s\n", outStr);
}
9번 주사위 2개를 던져 나온 숫자 10개를 나열하는 코드
#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int throwDice(){
return rand() %6+1;
}
void main(){
short int diceNum[10] = {0,};
int i=0;
char dup = 'N';
printf("**주사위 2개 던지기 시작**\n\n");
srand((unsigned)time(NULL));
int dice1, dice2;
int equalCount = 0;
while(1){
dice1 = throwDice();
dice2 = throwDice();
equalCount++;
if(dice1==dice2){
if(i==10) break;
diceNum[i] = dice1;
i++;
}
}
printf("같은 숫자가 나온 순서 ==> ");
for(i=0;i<10;i++){
printf("%2d", diceNum[i]);
}
printf("\n총 주사위 던진 횟수 %d", equalCount);
printf("\n\n");
}
11장 표춘 입출력과 파일 입출력
비밀번호 확인
#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(){
char password[5];
char input[5];
printf("비밀번호를 입력하세요");
for(int i=0;i<5;i++){
password[i]= getchar();
}
printf("비밀번호 확인");
for(int i=0;i<5;i++){
input[i]= getchar();
}
if(strncmp(password,input,4)==0){
printf("\n비밀번호 동일\n");
}
else{
printf("\n틀림\n");
}
}
'C' 카테고리의 다른 글
C - strcpy, strncpy / strcmp (0) | 2022.05.23 |
---|---|
C - 자료구조 연결리스트 (0) | 2022.05.19 |
C - 자료구조 덱 (0) | 2022.05.18 |
C - 자료구조 큐 (0) | 2022.05.03 |
C - 자료구조 스택 (0) | 2022.04.07 |