책문제풀기 - 2

2021. 11. 9. 20:35코딩/C언어

문제1.

아래 source code을 참고 하여 strcat과 my_strcat(page 349 참고)

sprintf함수를 활용하여 1.2와 같은 결과가 출력 되도록 program을 작성 하시오

 

참조

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

int main()
{
	char str1[] = "str1";
	char str2[] = "str2";
	char str3[] = "str2";

	char output[100] = { 0 };
}

 

코드

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
char* my_strcat(char* pd, char* ps);

int main()
{
	char str1[] = "str1";
	char str2[] = "str2";
	char str3[] = "str2";

	char output[100] = { 0 };

	printf("--- strcat 으로 문자열 합치기--- ");
	strcat(output, str1);
	strcat(output, str2);
	strcat(output, str3);
	printf("\n%s\n", output);
	output[0] = 0;

	printf("---my_strcat으로 문자열 합치기 ---");
	my_strcat(output, str1);
	my_strcat(output, str2);
	my_strcat(output, str3);
	printf("\n%s\n", output);

	printf("---sprintf으로 문자열 합치기 ---");
	sprintf(output, "%s%s%s\n", str1, str2, str3);
	printf("\n%s", output);
}
	char* my_strcat(char* pd, char* ps)
	{
		char* po = pd;

		while (*pd != '\0')
		{
			pd++;
		}
		while (*ps != '\0')
		{
			*pd = *ps;
			pd++;
			ps++;
		}
		*pd = '\0';
		return po;
	}

 

352P - 2번문제

define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <string.h>


int main(void)

{

char str[80];

strcpy(str, "wine");

strcat(str, "apple");

strncpy(str, "pear", 1);

printf("%s, %d\n", str, strlen(str));

return 0;

}

353P - 3번문제

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

int main()
{
	char str[50];
	
	
	while (1)
	{
		printf("단어입력 :");

		scanf("%s", str);
		if (strcmp(str, "exit") == 0)
		{
			break;
		}

		printf("입력한 단어 :%s\n", str);
		
		if (strlen(str) > 5)
		
		{
			int i;
			for (i = 5; i < strlen(str); i++)
			{
				str[i] = '*';
			}
		}
		printf("생략한 단어 : %s\n", str);

		
	}
	return 0;
}

4번문제

임의의 한 단어를 입력받아서 4.1과 같이 단어를 역순으로 출력 하는 program을 작성하되

while(1) 문을 활용 하여 연속 실행 되도록 한다. (단 사용자로 부터 "exit"를 입력 하면

프로그램 실행을 종료 한다.

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

int main(void)
{

    char str1[50] = { 0, };
    char strtemp[50] = { 0, };
    printf("---- reverse program ----\n");
    while (1)
    {

        printf("1단어를 입력:");
        scanf("%s", str1);
        if (strcmp(str1, "exit") == 0)
        {
            break;
        }
        
        int strlen1 = strlen(str1);

        for (int i = 0; i < strlen1+1; i++)
        {
            strncat(strtemp, &str1[strlen1 - i], 1);
        }

        printf("거꾸로 출력%s\n", strtemp);

        for (int i = 0; i < 50; i++)
        {
            strtemp[i] = 0;
        }
        
    }
    return 0;
    }

5번문제

5.1 10으로 그대로 출력 된 이유는 무엇인가요

매개변수 a의 메모리주소에 저장된값을 main함수에 넘겨주지않았기 때문이다

main함수의 변수 a가 증가하기를 원한다면

add_ten 함수가 증가시킨값을 반환하여 main함수 a에 다시 대입하는 방법을 사용해야합니다.

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

void add_ten(int *pa);

int main(void)
{
	int a = 10;

	add_ten(&a);
	printf("a : %d\n", a);

	return 0;

}

void add_ten(int *pa)
{
	*pa = *pa + 10;
}

 

6번문제

6-1. c언어에서 call-by-value와 call-by-reference의 차이점은 무엇 인가 ?

call by value 는 값에 의한 호출

call by reference 는 주소에 의한 호출

value는 복사된 값은 주소값이아닌 복사 값이기 때문에 함수내의 a를 조작하여도 원본 변수 a는 변하지 않는다 반면

reference는 원본 a값을 호출한것이기 때문에 함수 내부에서 a값이 변경되면 실제 값도 변경된다.

6-2 c언어에서 call-by-reference를 쓰는 이유는 무엇인가 ?

큰 이유는 메모리의 절감이다

value를사용시 복사를 해야하기때문에 메모리를 많이 차지하는 반면

reference는 포인터 변수정도의 메모리만 차지하기 때문에 변수에 대한 할당량이 클수록 포인터 변수의 유용성은 증가한다.

 

7번문제 379P

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

void add_by_pointer(int* pa, int* pb, int* pr)
{
	*pr = *pa + *pb;
}
int main(void)
{
	int a = 10, b = 20, res = 0;
	add_by_pointer(&a, &b, &res);
	printf("%d", res);
	return 0;
}

8번문제 380P

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

void input_data(int* pa, int* pb);
void swap_data(void);
void print_data(int a, int b);

int a, b;

int main(void)
{
	input_data(&a, &b);
	swap_data();
	print_data(a, b);

	return 0;
}

void input_data(int* pa, int* pb)
{
	printf("두 정수 입력:");
	scanf("%d %d", pa, pb);
}

void swap_data(void)
{
	int c = 0;
	c = a;
	a = b;
	b = c;
}

void print_data(int a, int b)
{
	printf("두 정수 출력 : %d, %d", a, b);
}

'코딩 > C언어' 카테고리의 다른 글

[자료구조] Circulr Queue / Sort(bubble, selection)  (0) 2021.11.09
책문제풀기 - 3  (0) 2021.11.09
책문제 풀기 - 1  (0) 2021.11.09