연재 완료/C Lang 예제코드 모음

C언어 연습문제 풀이 CH11 (13~16)

라이피 (Lypi) 2018. 6. 11. 21:19
반응형

13. 소문자/대문자로 구성된 문자열을 입력받아 모두 대문자로 변환하거나, 모두 소문자로 변환하는 프로그램을 작성하라.

//소문자/대문자로 구성된 문자열을 입력받아 모두 대문자로 변환하거나, 모두 소문자로 변환하는 프로그램을 작성하라.

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

int main()
{
	do {
		char buffer[100];
		printf("대소문자가 섞인 영어 문장을 입력하세요. : "); scanf_s("%s", buffer, 100);
		
		int input;

		printf("원하는 동작을 선택하세요. \n");
		printf("1. 소문자를 대문자로 변경 \n");
		printf("2. 대문자를 소문자로 변경 \n");
		scanf_s("%d", &input);

		do {
			switch (input) {
			case 1:
				_strupr_s(buffer);
				printf("%s \n", buffer);
				break;

			case 2:
				_strlwr_s(buffer);
				printf("%s \n", buffer);
				break;

			default:
				printf("잘못 입력하셨습니다");
			}
		} while (input != 1 && input != 2);
		
		printf("종료하시려면 esc키를 누르세요.\n계속하시려면 그 외 키를 누르세요. \n");
	} while (27 != _getch());
}


14. 문자열 "the beautiful mind"에서 단어의 첫 문자만 대문자로 변환하여 출력하는 프로그램을 작성하라.

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

int main()
{
	char after[30] = "the beautiful mind";
	char before[30] = " ";
	char* context;
	char* p;

	p = strtok_s(after, " ", &context);
	p[0] =toupper(p[0]);
	strcpy_s(before,30, p);

	while (p != NULL) {
		p =strtok_s(context, " ", &context);
		strcat_s(before, " ");
		if(p != NULL) {
			p[0] = toupper(*p);
			strcat_s(before, p);
		}
	}

	printf("%s \n", before);
}


15. 표준 입력으로부터 여러 라인을 읽고 각 라인을 거꾸로 표시하여 출력하는 프로그램을 작성하라.

//표준 입력으로부터 여러 라인을 읽고 각 라인을 거꾸로 표시하여 출력하는 프로그램을 작성하라.

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

struct node {
	char* d;
	node* prev;
	node* next;
};

typedef node* LINK;
typedef char* DATA;
typedef node ELEMENT;

LINK hp = NULL;
LINK tp = NULL;


void string_to_list(LINK p, DATA str);
void wrt_rev_list(LINK tp);


int main()
{
	char buffer[100];
	node* bufP = NULL;
	int cont;

	do {
		printf("문장을 입력하세요 : "); scanf_s("%s", buffer, 100);
		string_to_list(bufP, buffer);
		printf("계속 입력하시려면 아무키나, 종료하시려면 esc를 누르세요. \n");
		cont = _getch();
		while (getchar() != '\n');
	}while (27 != cont);

	wrt_rev_list(tp);
}

void string_to_list(LINK p, DATA str)
{
	LINK head;

	if (strlen(str) == 0) {
		printf("연결할 데이터가 없습니다. \n");
	}
	head = (LINK)malloc(sizeof(ELEMENT));
	
	head->d = (char*)malloc(strlen(str)+1);
	strcpy_s(head->d, strlen(str)+1, str);
	//strcpy_s의 중간 값은 넣고 싶은 크기를 입력해줘야 한다.

	if (hp == NULL) {
		head->prev = NULL;
		head->next = NULL;

		hp = head;
		tp = head;
	}
	else {
		tp->next = head;
		head->prev = tp;
		head->next = NULL;
		tp = head;
	}
}


void wrt_rev_list(LINK tp)
{
	LINK p;
	for (p = tp; p != NULL; p = p->prev) {
		printf("%s",p->d);
	}
}


16. 3사람의 이름을 영문으로 입력받아 abc순으로 정렬하여 출력하는 프로그램을 작성하라.

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

int main()
{
	int iName_count = 0;

	printf("입력할 사람 수를 입력하세요 : "); scanf_s("%d", &iName_count);
	
	char** Name_array = NULL;
		
	Name_array = (char**)malloc(sizeof(char*)*iName_count);

	for (int i = 0; i < iName_count; i++) {
		Name_array[i] = (char*)malloc(sizeof(char) * 10);
	}

	for (int i = 0; i < iName_count; i++) {
		printf("%d번째 사람의 이름을 영문자로 입력하세요 (최대 10글자) : ", i + 1); scanf_s("%s", Name_array[i],10);
	}

//오름차순으로 정렬	
	for (int i = 0; i < iName_count - 1; i++) {
		for (int k = i + 1; k < iName_count; k++) {
			if (strcmp(Name_array[i], Name_array[k]) > 0) {
				char temp[10] = { 0 };
				strcpy_s(temp, 10, Name_array[i]);
				strcpy_s(Name_array[i], 10, Name_array[k]);
				strcpy_s(Name_array[k], 10, temp);
			}		
		}
	}

	for (int i = 0; i < iName_count; i++) {
		printf("%s \n", Name_array[i]);
	}
}


반응형