0609 - ccyomni/ex250310_second GitHub Wiki

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
	// 1. 문자열을 입력 받아
	char buf[1000];
	gets_s(buf, 1000);
	// 2. 힙메모리 생성하고 문자열 복사
	char* s = NULL;
	s = (char*)malloc(strlen(buf)+1);
	s[0] = buf[0];
	s[1] = buf[1];
	s[2] = buf[2];
	s[3] = buf[3];
	s[4] = buf[4];
	s[5] = buf[5];
	// 3. 문자열을 출력
	printf("string : %s\n", buf);
	printf("string : %s\n", s);
	// 4. 힙메모리 삭제
}
  • 시험
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	// 1. 문자열을 입력 받아
	char buf[1000];
	printf("문자열 입력: ");
	gets_s(buf, 1000);
	// 2. 힙메모리 생성하고 문자열 복사
	char* s = NULL;
	s = (char*)malloc(strlen(buf)+1);
	strcpy(s, buf);
	// 3. 문자열을 출력
	printf("string : %s\n", buf);
	printf("string : %s\n", s);
	// 4. 힙메모리 삭제
	free(s);
}

문자열 입력을 2개 받을 때

#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	char buf[1000];
	printf("문자열 입력: ");
	gets_s(buf, 1000);
	char* s1 = NULL;
	s1 = (char*)malloc(strlen(buf)+1);
	strcpy(s1, buf);

	printf("문자열 입력: ");
	gets_s(buf, 1000);
	char* s2 = NULL;
	s2 = (char*)malloc(strlen(buf) + 1);
	strcpy(s2, buf);

	printf("string : %s\n", s1);
	printf("string : %s\n", s2);

	free(s1);
	free(s2);
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	// char* s1;
	// char* s2;
	char* s[100] = { NULL };
	
	char buf[1000];

	printf("문자열 입력: ");
	gets_s(buf, 1000);
	s[0] = (char*)malloc(strlen(buf) + 1);
	strcpy(s[0], buf);

	printf("문자열 입력: ");
	gets_s(buf, 1000);
	s[1] = (char*)malloc(strlen(buf) + 1);
	strcpy(s[1], buf);

	printf("string : %s\n", s[0]);
	printf("string : %s\n", s[1]);

	free(s[0]);
	free(s[1]);
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	char* s[100] = { NULL };
	int count = 0;

	
	char buf[1000];

	printf("문자열 입력: ");
	gets_s(buf, 1000);
	s[0] = (char*)malloc(strlen(buf) + 1);
	strcpy(s[0], buf);
	++count;

	printf("문자열 입력: ");
	gets_s(buf, 1000);
	s[1] = (char*)malloc(strlen(buf) + 1);
	strcpy(s[1], buf);
	++count;

	for (int i = 0; i < count; ++i)
		printf("string[%d] : %s\n", i, s[i]);

	for (int i = 0; i < count; ++i)
		free(s[i]);
}
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	char* s[100] = { NULL };
	int count = 0;

	
	char buf[1000];

	printf("문자열 입력: ");
	gets_s(buf, 1000);
	s[count] = (char*)malloc(strlen(buf) + 1);
	strcpy(s[count], buf);
	++count;

	printf("문자열 입력: ");
	gets_s(buf, 1000);
	s[count] = (char*)malloc(strlen(buf) + 1);
	strcpy(s[count], buf);
	++count;

	printf("문자열 입력: ");
	gets_s(buf, 1000);
	s[count] = (char*)malloc(strlen(buf) + 1);
	strcpy(s[count], buf);
	++count;

	for (int i = 0; i < count; ++i)
		printf("string[%d] : %s\n", i, s[i]);

	for (int i = 0; i < count; ++i)
		free(s[i]);
}
#pragma warning (disable:4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    char* s[100] = { NULL };
    int count = 0;
    int run = 1;

    while (run) {
        char buf[1000];
        printf("문자열 입력: ");
        gets_s(buf, 1000);

        if (strcmp("exit", buf) == 0)
            run = 0;
        else {
            s[count] = (char*)malloc(strlen(buf) + 1);
            strcpy(s[count], buf);
            ++count;
        }
    }

    for (int i = 0; i < count; ++i)
        printf("[%d] : %s\n", i, s[i]);

    return 0;
}

return 0 없앤 버전으로 수정하기

#pragma warning (disable:4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct StringArray
{
    char* s[100];
    int count = 0;
};
int main()
{
	StringArray sa = { 0 };

	int run = 1;
	
	while (run) {
		char buf[1000];
		printf("문자열 입력: ");
		gets_s(buf, 1000);
		if (strcmp("exit", buf) == 0)
			run = 0;
		else {
			sa.s[sa.count] = (char*)malloc(strlen(buf) + 1);
			strcpy(sa.s[sa.count], buf);
			++sa.count;
		}
	}
	for (int i = 0; i < sa.count; ++i)
		printf("[%d] : %s\n", i, sa.s[i]);
	for (int i = 0; i < sa.count; ++i)
		free(sa.s[i]);
}
#include <stdlib.h>
struct StringArray
{
    char* s[100];
    int count = 0;
};
void FreeStringArray(StringArray* psa)
{
	for (int i = 0; i < psa->count; ++i)
		free(psa->s[i]);
}
int main()
{
	StringArray sa = { 0 };

	int run = 1;
	
	while (run) {
		char buf[1000];
		printf("문자열 입력: ");
		gets_s(buf, 1000);
		if (strcmp("exit", buf) == 0)
			run = 0;
		else {
			sa.s[sa.count] = (char*)malloc(strlen(buf) + 1);
			strcpy(sa.s[sa.count], buf);
			++sa.count;
		}
	}
	for (int i = 0; i < sa.count; ++i)
		printf("[%d] : %s\n", i, sa.s[i]);
	
	FreeStringArray(&sa);
}
#pragma warning (disable:4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct StringArray
{
    char* s[100];
    int count = 0;
};
void FreeStringArray(StringArray* psa)
{
	for (int i = 0; i < psa->count; ++i)
		free(psa->s[i]);
}
void PrintStringArray(StringArray* psa)
{
	for (int i = 0; i < psa->count; ++i) {
		printf("%d: %s\n", i, psa->s[i]);
	}
}
int main()
{
	StringArray sa = { 0 };

	int run = 1;
	
	while (run) {
		char buf[1000];
		printf("문자열 입력: ");
		gets_s(buf, 1000);
		if (strcmp("exit", buf) == 0)
			run = 0;
		else {
			sa.s[sa.count] = (char*)malloc(strlen(buf) + 1);
			strcpy(sa.s[sa.count], buf);
			++sa.count;
		}
	}
	PrintStringArray(&sa);
	
	FreeStringArray(&sa);
}

시험에는 안 나옴

#pragma warning (disable:4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct StringArray
{
    char* s[100];
    int count = 0;
};
void FreeStringArray(StringArray* psa)
{
	for (int i = 0; i < psa->count; ++i)
		free(psa->s[i]);
}
void PrintStringArray(StringArray* psa)
{
	for (int i = 0; i < psa->count; ++i) {
		printf("%d: %s\n", i, psa->s[i]);
	}
}
void InputBuffer(char* buf, int size)
{
	printf("문자열 입력: ");
	gets_s(buf, size);
}
int IsExit(const char* buf)
{
	return strcmp("exit", buf) == 0;
}
void AddStringAway(StringArray* psa, const char* buf)
{
	psa->s[psa->count] = (char*)malloc(strlen(buf) + 1);
	strcpy(psa->s[psa->count], buf);
	++psa->count;	
}
int main()
{
	StringArray sa = { 0 };

	int run = 1;
	
	while (run)
	{
		char buf[1000];
		InputBuffer(buf, 1000);
		if (IsExit(buf))
			run = 0;
		else
			AddStringAway(&sa, buf);
	
	}
	PrintStringArray(&sa);

	FreeStringArray(&sa);
}

시험에는 안 나옴

배열 삭제

#include <stdio.h>

int main() {
    int arr[10] = { 10, 20, 30, 40, 50 };
    int count = 5;

    int delIndex = 1;
    arr[delIndex] = arr[count - 1];
    --count;

    for (int i = 0; i < count; i++)
        printf("[%d] : %d\n", i, arr[i]);

    return 0;
}
⚠️ **GitHub.com Fallback** ⚠️