๋”ฐ๋ฐฐ์”จ

๋”ฐ๋ฐฐ์”จ 11.3 ~

์•„์˜ณ์ด 2021. 1. 27. 19:08

11.3 ๋ฌธ์ž์—ด์˜ ๋ฐฐ์—ด

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
	const char* mythings[5] = {			//	ํฌ์ธํ„ฐ๋ฐฐ์—ด
		"Dancing in the rain",
		"Counting apples",
		"Watching movies with friends",
		"Writing sad letters",
		"Studying the C language"
	};

	char yourthings[5][40] = {			//	2์ฐจ์›๋ฐฐ์—ด
		"Studying the C++ language",
		"Eating",
		"Watcing Netflix",
		"Walking around till dark",
		"Deleting spam emails"
	};

	const char* temp1 = "Dancing in the rain";
	const char* temp2 = "Studying the C++ language";

	printf("%d %u %u\n", mythings[0], (unsigned)mythings[0], (unsigned)temp1);
	printf("%d %u %u\n", yourthings[0], (unsigned)yourthings[0], (unsigned)temp2);

	printf("\n");

	printf("%-30s %-30s\n", "My Things:", "Your Things:");
	for (int i = 0; i < 5; i++)
		printf("%-30s %-30s\n", mythings[i], yourthings[i]);

	printf("\nsizeof mythings: %zd, sizeof yourthings: %zd\n",
		sizeof(mythings), sizeof(yourthings));		//	20	200

	for (int i = 0; i < 100; i++)
		printf("%c", mythings[0][i]);
	printf("\n");
	printf("\n");

	for (int i = 0; i < 200; i++)
		printf("%d", (int)yourthings[0][i]);
	printf("\n");

	for (int i = 0; i < 200; i++)
		printf("%c", yourthings[0][i]);
	printf("\n");
	printf("\n");

	//	Not a good ideato take advantageof this property

	return 0;
}

 

 

11.4 ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•๋“ค

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

#define STRLEN 81

char* custom_string_input(char* st, int n);

int main()
{
	/* Creationg Space */

	//char* name = "";	//	Error at RUN-TIME
	char name[128];
	int result = scanf("%s", name);

	/*
		scanf() vs gets()
		scanf() reads one word
		gets() reads one line and remove \n and add \0
	*/
	{
		char words[STRLEN] = "";	//	Warning without initialization
		gets(words);		//	gets receives pointer. No ideawhen string ends.
		//gets_s(words, sizeof(words));		//	C11	//	๋ฐฐ์—ด์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์ •ํ•ด์ค€๋‹ค
		//int result = scanf("%s", words);
		printf("START\n");
		printf("%s", words);	//	no \natthe end
		puts(words);			//	puts() adds \n at the end
		puts("END.");
	
		//	tyr char words[5];	//	5๋ณด๋‹ค ํฐ ๋ฌธ์ž์—ด์„ ๋ฐ›์•„๋“ค์ผ์‹œ ๋Ÿฐํƒ€์ž„์—๋Ÿฌ ๋ฐœ์ƒ
	}

	/* fgets() and fputs() */
	{
		char words[STRLEN] = "";
		fgets(words, STRLEN, stdin);	//	dose not remove \n
		// ์ •ํ•ด์ง„ ์‚ฌ์ด์ฆˆ๋ณด๋‹ค ํฐ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋“ค์–ด์™€๋„ ๋ฐ›์•„๋“œ๋ฆผ

		// replace '\n' with '\0'
		int i = 0;
		while (words[i] != '\n' && words[i] != '\0')
			i++;
		if (words[i] == '\n')
			words[i] = '\0';
		
		fputs(words, stdout);			// dose not add \n
		fputs("END", stdout);

	}

	/* Small array */
	{
		char small_array[5];
		puts("Enter long strings:");
		fgets(small_array, 5, stdin);	//	FILE *_Stream
		fputs(small_array, stdout);		//	123456 ์ž…๋ ฅ์‹œ 1234 ์ถœ๋ ฅ - ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋˜๋„ OK
	}

	/* Repeating short reading */
	{
		char small_array[5];
		puts("Enter long strings:");
		while (fgets(small_array, 5, stdin) != NULL && small_array[0] != '\n')
			fputs(small_array, stdout);
			//puts(small_array);	//	\0๊ฐ€ ์ถ”๊ฐ€๋˜ 4๋ฌธ์ž(+\0)์”ฉ ์ถœ๋ ฅ๋˜๊ณ  ์ค„๋ฐ”๊ฟˆ
	}

	/* scanf() */
	{
		char str1[6], str2[6];
		int count = scanf("%5s %5s", str1, str2);
		//int count = scanf("%6s %6s", str1, str2);	//	RUN-TIME error
		//int count = scanf_s("%5s %5s", str1, 6, str2, 6);
		printf("%s|%s \n", str1, str1);
	}

	return 0;
}

	/* Example of custom input function */
char* custom_string_input(char* st, int n)
	{
		char* ret_ptr;
		int i = 0;

		ret_ptr = fgets(st, n, stdin); //fgets๋กœ ๋ฐ›๋Š” ๋ฌธ์ž์—ด ํฌ์ธํ„ฐ ์ฃผ์†Œ ๊ทธ ์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์„œ ret_ptr๋กœ ๋Œ€์ž…
		if (ret_ptr)
		{
			while (st[i] != '\n' && st[i] != '\0') //๋์— \n ์žˆ์œผ๋ฉด \0์œผ๋กœ ๋ณ€ํ™˜
				i++;
			if (st[i] == '\n')	//๋์— \n ์žˆ์œผ๋ฉด \0์œผ๋กœ ๋ณ€ํ™˜
				st[i] = '\0';
			else
				while (getchar() != '\n') //clear buffer
					continue;
		}
		return ret_ptr;
	}

 

 

11.5 ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•๋“ค

#define TEST "Astring from #define."
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

void custom_put(const char* str);	//	putchar()์„ ์ด์šฉํ•ด์„œ \n ํฌํ•จ์‹œํ‚ค์ง€ ์•Š๋Š” put() ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ
int custim_put2(const char* str);	//	์œ„์˜ ํ•จ์ˆ˜์— \n์„ ์ถ”๊ฐ€ํ•˜๊ณ  return๊ฐ’์œผ๋กœ ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

int main()
{
	/* puts() : add \nat the end */ 
	{
		char str[60] = "String array initialized";
		const char* ptr = "A pointer initialized";

		puts("String without \\n");
		puts("END");
		puts("TEST");
		puts(TEST + 5);
		puts(&str[3]);
		//puts(str[3]);	//	Error
		puts(ptr + 3);
	}

	/* string without \0 */
	{
		char str[] = { 'H', 'I', '!' };	//	no \0 at the end	//	์“ฐ๋ ˆ๊ธฐ๊ฐ’ ์ถœ๋ ฅ
		puts(str);	//	VS warns you!
	}

	/* puts() and fputs() */
	{
		char line[100];
		while (gets(line))
			puts(line);
	
		char line[100];
		while (fgets(line, 100, stdin))
			fputs(line, stdout);
	}

	/* printf() */
	{
		char str[] = "Just do it. do it!";
		printf("%s\n", str);		//	\n added
		puts(str);

		char input[100] = "";
		int ret = scanf("%10s", input);	//	Input: "Just do it, do it!\n" (Note %10s)
		printf("%s\n", input);			//	Output : "Just"
		ret = scanf("%10s", input);		//	Reads remaning buffer
		printf("%s\n", input);			//	Output : "do"
	}

	/* Custom function */
	{
		custom_put("Just ");
		custom_put("do it!");

		printf("%d\n", custim_put2("12345"));
	}

	return 0;
}

void custom_put(const char* str)
{
	while (*str != '\0')	//	while(*str)
		putchar(*str++);
}

int custim_put2(const char* str)
{
	int count = 0;
	while (*str)
	{
		putchar(*str++);
		count++;
	}
	putchar('\n');

	return count;
}

 

 

11.6 ๋‹ค์–‘ํ•œ ๋ฌธ์ž์—ด ํ•จ์ˆ˜๋“ค

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

void fit_str(char*, unsigned int);
void my_strcat(char*, char*);
int my_strcmp(char*, char*);

int main()
{
	/* strlen() : return lengthof the string */
	{
		char msg[] = "just do it!";
		puts(msg);
		printf("Length %d\n", strlen(msg));
		fit_str(msg, 4);
		puts(msg);
		printf("Length %d\n", strlen(msg));
	}

	/* strcat() and strncat() : string concatenation */
	{
		char str1[100] = "Fisrt string";
		char str2[] = "Second string";

		strcat(str1, ", ");
		strcat(str1, str2);
		strncat(str1, str2, 2);	//	Append 2 characters
		puts(str1);
	}

	/* strcmp() and strncmp() : compare strinfs (not characters) */
	{
		printf("%d\n", strcmp("A", "A"));					//0
		printf("%d\n", strcmp("A", "B"));					//-1
		printf("%d\n", strcmp("B", "A"));					//1
		printf("%d\n", strcmp("Hello", "Hello"));			//0
		printf("%d\n", strcmp("Banana", "Bananas"));		//-1
		printf("%d\n", strcmp("Bananas", "Banana"));		//1
		printf("%d\n", strncmp("Bababas", "Banana", 6));	//0
	}

	/* strcpy() and strncpy() */
	{
		char dest[100] = "";
		char source[] = "Start programing!";
		//dest = source;		//Error ๋ฐฐ์—ด๋ช…์€ ์ƒ์ˆ˜
		//dest = "Start something";	//Error ๋ฐฐ์—ด๋ช…์€ ์ƒ์ˆ˜
		strcpy(dest, source);
		strncpy(dest, source, 5);	//	'\0' is NOT added
		strcpy(dest, source + 6);
		strcpy(dest, source);
		strcpy(dest + 6, "coding!");
		puts(dest);
	}

	/* sprintf() */
	{
		char str[100] = "";
		int i = 123;
		double d = 3.14;
		sprintf(str, "%05d.png %f", i, d);
		puts(str);
	}

	/* There are more functions */
	{
		printf("%s\n", strchr("Hello, World", 'W'));	//W๋กœ ์‹œ์ž‘ํ•˜๋Š” ์œ„์น˜๋ฅผ ์ฐพ์•„์„œ ํฌ์ธํ„ฐ๋กœ ๋ฆฌํ„ด
		
		printf("%s\n", strbrk("Hello, World", "ABCDE"));	//ABCDE์ค‘์— ์žˆ๋Š” ๊ธ€์ž๋กœ ์‹œ์ž‘๋˜๋Š” ์œ„์น˜๋ฅผ
		printf("%s\n", strbrk("Hello, World", "abcde"));
		
		printf("%s\n", strrchr("Hello, World, Hollo, World", 'l'));//	last occurrence

		printf("%s\n", strstr("Hello, World", "wor"));	//๋ฌธ์ž์—ด๋ผ๋ฆฌ ๋น„๊ต (wor ์—†์œผ๋‹ˆ null)
		printf("%s\n", strstr("Hello, World", "Wor"));
	}

	return 0;
}

void fit_str(char* str, unsigned int size)
{
	if (strlen(str) > size)
		str[size] = '\0';
}

void my_strcat(char* str1, char* str2)
{
	while (*str1 != '\0')
		str1++;

	if (*str1 == '\0')
		while (*str2 != '\0')
			*str1++ = *str2++;
}

int my_strcmp(char* str1, char* str2)
{
	int cmp, result = 0;

	while (*str1 != '\0' && *str1 == * str2)
	{
		str1++;
		str2++;
	}
	cmp = *str1 - *str2;
	result = (cmp > 0) ? 1 : (cmp < 0) ? -1 : 0;
	
	return result;
}

 

 

11.7 ์„ ํƒ ์ •๋ ฌ ๋ฌธ์ œ ํ’€์ด Selection Sort

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

/*
	Selection Sort Algorithm
	https://www.geeksforgeeks.org/selection-sort/

	64 25 12 22 11	(min_idx = 0)
	   64			(min_idx = 0)
	   25			(min_idx = 1)
	      25		(min_idx = 1)
		  12		(min_idx = 2)
		     12		(min_idx = 2)
			    11	(min_idx = 4)
	11 25 12 22 64	(swap arr[0] and arr[4])
*/

void swap(int* xp, int* yp);
void printArray(int arr[], int size);
void selectionSort(int arr[], int n);

int main()
{
	int arr[] = { 64, 25, 12, 22, 11 };
	int n = sizeof(arr) / sizeof(arr[0]);

	selectionSort(arr, n);	//	ascending order

	printArray(arr, n);

	return 0;
}

void printArray(int arr[], int size)
{
	int i;
	for (i = 0; i < size; i++)
		printf("%d ", arr[i]);
		printf("\n");
}

void swap(int* xp, int* yp)
{
	int temp = *xp;
	*xp = *yp;
	*yp = temp;
}

void selectionSort(int arr[], int n)
{
	int i, j, min_idx;

	for (i = 0; i < n - 1; i++)	//Note n - 1
	{
		min_idx = i;
		for (j = i + 1;j < n; j++)
		{
			if (arr[min_idx] > arr[j])
				min_idx = j;
		}
		swap(&arr[min_idx], &arr[i]);
	}
}

 

 

11.8 ๋ฌธ์ž์—ด์˜ ํฌ์ธํ„ฐ๋ฅผ ์ •๋ ฌํ•˜๊ธฐ

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>	//	strcmp()


void swap(char** xp, char** yp);	//Note: Optional
void printArray(char arr[], int size);
void selectionSort(char arr[], int n);

int main()
{
	char* arr[] = { "Cherry", "AppleBee", "Pineapple", "Apple", "Orange" };
	int n = sizeof(arr) / sizeof(arr[0]);

	selectionSort(arr, n);	//	ascending order

	printArray(arr, n);

	return 0;
}

void printArray(char arr[], int size)
{
	int i;
	for (i = 0; i < size; i++)
		puts(arr[i]);
	printf("\n");
}

void swap(char** xp, char** yp)
{
	char* temp = *xp;
	*xp = *yp;
	*yp = temp;
}

void selectionSort(char** arr[], int n)
{
	int i, j, min_idx;

	for (i = 0; i < n - 1; i++)	//
	{
		min_idx = i;
		for (j = i + 1;j < n; j++)
		{
			if (strcmp(arr[j], arr[min_idx]) < 0)
				min_idx = j;
		}
		swap(&arr[min_idx], &arr[i]);
	}
}

-11.7์˜ ์˜ˆ์ œ์—์„œ selectionSortํ•จ์ˆ˜์˜ ๋‚ด์šฉ์„ strcmp๋กœ ์กฐ๊ธˆ๋งŒ ๋ฐ”๊ฟ”์ค€ ์ฝ”๋“œ

 

 

11.9 ๋ฌธ์ž ํ•จ์ˆ˜๋ฅผ ๋ฌธ์ž์—ด์— ์‚ฌ์šฉํ•˜๊ธฐ ctype.h

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>	//	toupper(), ispunct(), ...

#define NUM_LIMIT 1024

void ToUpper(char*);
int PunctCount(const char*);

int main()
{
	char line[NUM_LIMIT];
	char* new_line = NULL;
	fgets(line, NUM_LIMIT, stdin);
	new_line = strchr(line, '\n');	//	find first newline
	if (new_line)
		*new_line = '\0';

	ToUpper(line);
	puts(line);
	printf("%d\n", Punctcount(line));

	return 0;
}

void ToUpper(char* str)
{	
	while (*str != '\0')
	{
		*str = toupper(*str);
		str++;
	}
}

int PunctCount(const char* str);
{
	int ct = 0;
	while (*str != '\0')
	{
		if (ispunct(*str))
			ct++;
		str++;
	}
	
	return ct;
}