Native/C

예제 1-16

aucd29 2013. 10. 2. 18:49
/***************************************************************************
*
*        Date            : 2005-04-09
*        Copyright        : aucd29
*        E-mail            : aucd29@daum.net
*
*        기존프로그램의 main을 수정해서 모든 행의 길이와 문자들을 출력하도록
*        하는 프로그램
*
***************************************************************************/


#include <stdio.h>

#define MAXLINE 1000
#define AR_MAX 10

int getline(char line[],int maxline);
void copy(char to[AR_MAX][MAXLINE],char from[],int *nCnt);
void ResultOut(char to[AR_MAX][MAXLINE], int nStrLen[]);

int main()
{
    int len;
// int max;
    char line[MAXLINE];
    char longest[AR_MAX][MAXLINE];
    int nStrLen[AR_MAX];
    int nCnt=0,i=0;

    for(i=0;i<AR_MAX;++i) nStrLen[i] = 0;    // 변수 초기화

    while((len = getline(line,MAXLINE)) > 0)
    {
        if(AR_MAX<nCnt) break;

        copy(longest, line, &nCnt);    // 문단 배열에 복사
        nStrLen[nCnt] = len;    // 단어 카운트

        ++nCnt;
    }

    if(nCnt >0)
    {
        ResultOut(longest, nStrLen);
    }
    return 0;
}

int getline(char s[], int lim)
{
    int c, i;

    for(i=0;i<lim-1 && (c=getchar()) != EOF && c!='\n'; ++i)
        s[i] = c;

    if(c == '\n')
    {
        s[i] = c;
        ++i;
    }

    s[i] = '\0';
    return i;
}

void copy(char to[AR_MAX][MAXLINE], char from[], int *nCnt)
{
    int i=0;
    
    // 글자를 복사한다.... 배열이므로 하나씩...
    while((to[*nCnt][i] = from[i]) != '\0')
        ++i;

}

void ResultOut(char to[AR_MAX][MAXLINE], int nStrLen[])
{
    int i;

    for(i=0;i<AR_MAX;++i)
    {
        if(nStrLen[i] > 0)
            printf("%d번째 글내용 : %s글자수 : %d\n", i, to[i], nStrLen[i]);
    }
}