๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Language/C++

[C์ž๋ฃŒ๊ตฌ์กฐ] ๋ฌธ์ž์—ด ์—ฐ์Šต

by ํƒœ์˜น 2021. 7. 31.

๐Ÿ‘‡ ํ•ด๋‹น ๊ฐ•์˜ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•จ

 

C๋กœ ๋ฐฐ์šฐ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ๋ฐ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์˜ˆ์ œ ์‹ค์Šต - ์ธํ”„๋Ÿฐ | ํ•™์Šต ํŽ˜์ด์ง€

์ง€์‹์„ ๋‚˜๋ˆ„๋ฉด ๋ฐ˜๋“œ์‹œ ๋‚˜์—๊ฒŒ ๋Œ์•„์˜ต๋‹ˆ๋‹ค. ์ธํ”„๋Ÿฐ์„ ํ†ตํ•ด ๋‚˜์˜ ์ง€์‹์— ๊ฐ€์น˜๋ฅผ ๋ถ€์—ฌํ•˜์„ธ์š”....

www.inflearn.com

 

์—ฐ์Šต1. ์‚ฌ์šฉ์ž ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ

 

๋ฐฉ๋ฒ•1) gets()

#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 20

int main(){
    /*๋ฐฉ๋ฒ•1 : gets์‚ฌ์šฉ - ์•ˆ์ „ํ•˜์ง€ ์•Š์Œ*/
    char input[BUFFER_SIZE];
    while (1) {
        printf("$ ");
        gets_s(input, BUFFER_SIZE);
        printf("%s:%d\n",input,strlen(input));
    }
}

โ—พ BUFFER_SIZE๊นŒ์ง€๋งŒ ์ฝ์–ด์•ผ ํ•˜๋Š”๋ฐ ์ •ํ•ด์ง„ ๋ฒ„ํผ์˜ ํฌ๊ธฐ๋ฅผ ์ดˆ๊ณผํ•ด์„œ ์ฝ์Œ -> ๋ฌธ์ œ๊ฐ€ ๋จ

 

๋ฐฉ๋ฒ•2) fgets()

/*๋ฐฉ๋ฒ•2 : fgets์‚ฌ์šฉ - ๋ฌธ์ œ์˜ ์š”๊ตฌ์‚ฌํ•ญ์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š์Œ*/
while (1) {
    printf("$ ");
    fgets(input, BUFFER_SIZE, stdin);
    input[strlen(input) - 1] = '\0';
    printf("%s:%d\n", input, strlen(input));
}

โ—พ ์„ธ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํŒŒ์ผํฌ์ธํ„ฐ, stdin๋Š” ํ‚ค๋ณด๋“œ ์ž…๋ ฅ
โ—พ ์ •ํ•ด์ง„ ๋ฒ„ํผ์˜ ํฌ๊ธฐ๋งŒ ์ฝ์Œ
โ—พ fgets๋Š” ์—”ํ„ฐ๋ฅผ ์น˜๋ฉด \n๊นŒ์ง€ ๋ฒ„ํผ์— ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฒ„ํผ๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ์ค„๋ฐ”๊ฟˆ๊นŒ์ง€ ์ถœ๋ ฅ๋˜์–ด๋ฒ„๋ฆผ -> ๊ทธ๋ž˜์„œ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ \0๋กœ ๋ณ€๊ฒฝํ•œ ๊ฒƒ
โ—พ ๋ฒ„ํผ๋ฅผ ๋Š์–ด์„œ ์ฝ์–ด๋ฒ„๋ฆผ -> ๋ฌธ์ œ์˜ ์ทจ์ง€์™€ ์–ด๊ธ‹๋‚จ

 

 

๋ฐฉ๋ฒ•3) getchar()

void main() {
	/*๋ฐฉ๋ฒ•3 : getchar๋ฅผ ์‚ฌ์šฉํ•œ ์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜ ์‚ฌ์šฉ*/
	while (1) {
		printf("$ ");
		read_line(input, BUFFER_SIZE);
		printf("%s:%d\n", input, strlen(input));
	}
}

int read_line(char str[], int limit) {
	int ch, i = 0;
	while ((ch = getchar()) != '\n') {
		if (i < limit)
			str[i++] = ch;
	}
	str[i] = '\0';
	return i;
}

โ—พ getchar()์˜ ๋ฆฌํ„ดํƒ€์ž…์€ char๊ฐ€ ์•„๋‹ˆ๋ผ intํ˜•์ž„

โ—พ ๊ฐ€์žฅ ์ •ํ™•ํ•œ ๋ฐฉ๋ฒ•!

 

 


 

์—ฐ์Šต2. ์—ฌ๋Ÿฌ ์กฐ๊ฑด์œผ๋กœ ๊ณต๋ฐฑ๋ฌธ์ž ์ œ๊ฑฐํ•˜๊ธฐ 

 

โ—พ ๋ฌธ์ œ ํ•ด๊ฒฐ ์›๋ฆฌ

 

1) ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

๋”๋ณด๊ธฐ

ํ•ด์„ค ๋ณด๊ธฐ ์ „์—๋Š” ๋ชฐ๋ž๋Š”๋ฐ ํ•ด์„ค๋ณด๊ณ  ๋‚˜๋‹ˆ๊นŒ compressed๋ฐฐ์—ด์—๋„ ์ตœ๋Œ€ ํฌ๊ธฐ๋งŒํผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์—ˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐ ๊ทธ ๋ถ€๋ถ„์ด ๋น ์ ธ๋ฒ„๋ ธ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜๋‹ค... input์— ํฌ๊ธฐ ์ œ์•ฝ์„ ๊ฑธ์–ด๋†จ๋„ค...๐Ÿ™„

#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 20

int main() {
	char input[BUFFER_SIZE], compressed[BUFFER_SIZE];

	while (1) {
		printf("$ ");
		int count = read_line(input, compressed, BUFFER_SIZE);
		printf("%s:%d\n", compressed, count);
	}

}

int read_line(char str[], char str2[], int limit) {

	int ch, i = 0;
	int count = 0;

	while ((ch = getchar()) != '\n') {
		if (i < limit)
			str[i++] = ch;
	}
	str[i] = '\0';


	for (int j = 0;j < strlen(str); j++) {
		if (j != 0 && str[j] == ' ' && str[j - 1] != ' ') {
			str2[count] = str[j];
			count++;
		}
		else if (str[j] != ' ') {
			str2[count] = str[j];
			count++;
		}
	}
	if (str2[count - 1] == ' ') 
		count--;
	str2[count] = '\0';

	return count;
}

 

 

2) ํ•ด์„ค

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define BUFFER_SIZE 20

int main() {
	char line[BUFFER_SIZE];

	while (1) {
		printf("$ ");
		int count = read_line_with_compression(line, BUFFER_SIZE);
		printf("%s:%d\n", line, count);
	}
	return 0;
}

int read_line_with_compression(char compressed[], int limit) {

	int ch, i = 0;

	while ((ch = getchar()) != '\n') {
		if (i < limit - 1 && (!isspace(ch) || i > 0 && !isspace(compressed[i - 1])))
			// !isspace(ch) : ๊ณต๋ฐฑ์ด ์•„๋‹ˆ๊ฑฐ๋‚˜
			// i > 0 && !isspace(compressed[i - 1]) : ๊ณต๋ฐฑ์ด์ง€๋งŒ ์ง์ „์˜ ๋ฌธ์ž๊ฐ€ ๊ณต๋ฐฑ์ด ์•„๋‹Œ ๊ฒฝ์šฐ
			compressed[i++] = ch;
	}
	if (i > 0 && isspace(compressed[i - 1]))
		i--;
	compressed[i] = '\0';

	return i;
}

โ—พ <ctype.h>์˜ isspace() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ

โ—พ isspace(ch)์€ ch!=' '์™€ ๊ฐ™์Œ

๋Œ“๊ธ€