๋ฐ๋ฐฐ์จ
๋ฐ๋ฐฐ์จ 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;
}