작은숲:위키노트/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.
 */
#include <stdio.h>
#include <string.h>
#define STR_MAX        81
#define STR_NUM        81
#define WORD_MAX    81
void str_print(char **str, int num);
int main()
{    char str[STR_MAX];
    char word[STR_NUM][WORD_MAX];
    char temp[STR_MAX];
    int i, j, k, num;
    printf("Enter a string: ");
    gets(str);
    printf("String: %s\n", str);
    for (i = 0, j = 0, num = 0; str[i] != '\0'; i++) {
        if (str[i] == ' ' || str[i] == '\n' || str[i] == '\t') {
            word[num][j] = '\0';
            num++;
            j = 0;
        }
        else {
            word[num][j] = str[i];
            j++;
        }
    }
    word[num][j] = '\0';
    for (i = 0; i <= num; i++)
        printf("Original Word %d: %s\n", i, word[i]);
    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;
            }
        }
    }
    str_print(word, num + 1);
    return 0;
}void str_print(char **str, int num)
{    int i;
    for (i = 0; i < num; i++)
        printf("String %d: %s\n", i, str[i]);
}
이 작은숲 문서의 출처는 위키노트의 위키노트/C 언어 예제/문자열 함수 문서입니다.