작은숲:위키노트/C 언어 예제/문자열을 단어로 쪼개는 함수와 정렬 함수
보이기
/**
* Copyright (c) 2001,2002 Yoon, Hyunho <hhyoon@kldp.org>
* http://mooo.org
* ----------------------------------------------------------------------
*
* LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
* word_split(), str_sort()
*
* int word_split(const char *str, char word[][WORD_MAX]);
* 1. 문자열을 매개변수로 받아서 그것을 단어 단위로 쪼개는 함수.
* 2. 단어를 저장하기 위해 이차원 배열을 이용.
* 3. 단어를 나누는 기준은 공백문자들이다.
*
* void str_sort(char word[][WORD_MAX], int num);
* 1. 문자열을 정렬하는 함수.
* 2. 문자열을 저장한 이차원 배열과 배열의 크기를 매개변수로 받아서,
* 이 문자열들을 정렬한다.
*
*/
#include <stdio.h>
#include <string.h>
#define STR_MAX 81
#define WORD_MAX 30
#define NUM_WORD 81
int word_split(const char *str, char word[][WORD_MAX]);
void str_sort(char word[][WORD_MAX], int num);
void main(void)
{ char str[STR_MAX]; /* 문자열을 저장할 배열 */
char word[NUM_WORD][WORD_MAX]; /* 단어들을 저장할 이차원 배열 */
int i, num_words;
/* 하나의 문장을 입력받는다. */
printf("Enter a string: ");
gets(str);
printf("\nString: %s\n\n", str);
/* 문장을 단어로 쪼개기 */
num_words = word_split(str, word);
for (i = 0; i < num_words; i++)
printf("Word %d: %s\n", i, word[i]);
printf("\n");
/* 단어들 정렬하기 */
str_sort(word, num_words);
for (i = 0; i < num_words; i++)
printf("Sorted Word %d: %s\n", i, word[i]);
}/**
* word_split()
*
* int word_split(const char *str, char word[][WORD_MAX]);
* 1. 문자열을 매개변수로 받아서 그것을 단어 단위로 쪼개는 함수.
* 2. 단어를 저장하기 위해 이차원 배열을 이용.
* 3. 단어를 나누는 기준은 공백문자들이다.
*/
int word_split(const char *str, char (*word)[WORD_MAX])
{ int i, words, chars;
int start_word = 0;
/* 문장의 끝을 만날 때까지 루프를 돈다. */
for (i = 0, words = 0, chars = 0; str[i] != '\0'; i++) {
/* 현재 글자가 공백문자인지 검사한다.
공백문자이면 새 단어의 시작의 시작이다. */
if (str[i] == ' ' || str[i] == '\n' || str[i] == '\t') {
/* 연속된 공백이 나올 것을 대비하여 chars가 ..
값을 가지고 있는지 검사한다. */
if (chars) {
word[words][chars] = '\0';
words++;
}
chars = 0;
}
else {
word[words][chars] = str[i];
chars++;
}
}
word[words][chars] = '\0'; /* 마지막 단어의 NULL 문자 추가 */
words++; /* 마지막 당언의 개수 추가 */
return words; /* 단어 갯수 리턴 */
}/*
* void str_sort(char word[][WORD_MAX], int num);
* 1. 문자열을 정렬하는 함수.
* 2. 문자열을 저장한 이차원 배열과 배열의 크기를 매개변수로 받아서,
* 이 문자열들을 정렬한다.
* 3. 이 함수에서는 문자열들을 저장하기 위해 이차원 배열을 사용했으므로,
* 포인터를 바로 저장시키는 방법을 사용할 수 없다.
* 따라서, strcpy()라는 함수를 사용하여 문자열을 `복사'하였다.
*/
/*
void str_sort(char word[][WORD_MAX], int num)
{ int i, j, k;
char temp[WORD_MAX];
for (i = 0; i < num - 1; i++) {
for (j = 0; j < num - 1; j++) {
for (k = 0; word[j][k] != '\0' && word[j + 1][k] != '\0'; k++) {
if (word[j][k] > word[j + 1][k]) {
strcpy(temp, word[j]);
strcpy(word[j], word[j + 1]);
strcpy(word[j + 1], temp);
}
break;
}
}
}
}*/
/*
* void str_sort(char word[][WORD_MAX], int num);
* 1. 문자열을 정렬하는 함수.
* 2. 문자열을 저장한 이차원 배열과 배열의 크기를 매개변수로 받아서,
* 이 문자열들을 정렬한다.
* 3. 이 함수에서는 문자열들을 저장하기 위해 이차원 배열을 사용했으므로,
* 포인터를 바로 저장시키는 방법을 사용할 수 없다.
* 따라서, strcpy()라는 함수를 사용하여 문자열을 `복사'하였다.
*/
void str_sort(char word[][WORD_MAX], int num)
{ int i, j;
char temp[WORD_MAX];
for (i = 0; i < num - 1; i++) {
for (j = 0; j < num - 1; j++) {
/* 두 문장을 비교하여 순서가 잘못 된 경우 바꾼다.
문장을 비교하기 위해 strcmp() 함수를 이용한다. */
if (strcmp(word[j], word[j + 1]) == 1) {
/* 문자열을 바꾸기 위해서 strcpy() 함수를 이용한다. */
strcpy(temp, word[j]);
strcpy(word[j], word[j + 1]);
strcpy(word[j + 1], temp);
}
}
}
}