본문 바로가기

Native/C

예제 1-18

/***************************************************************************
*
*        Date            : 2005-04-09
*        Copyright        : aucd29
*        E-mail            : aucd29@daum.net
*
*        파일의 각 행에서 뒷부분의 공백과 tab을 제거하고 전체가 공백
*        인 행을 삭제하는 프로그램
*
***************************************************************************/


#include <stdio.h>
#include <string.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 BlankCheck(char line[MAXLINE],int *len);

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;
        
        if(BlankCheck(line,&len))
        {
            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]);
    }
}

int BlankCheck(char tmp[], int *len)
{
    int tmplen = *len;
    int i;

    //int cnt=0;
//    printf("%d\n",tmplen);

    for(i=tmplen;i>=0;--i)
    {
        //printf("\n%d:%c",i,tmp[i]);

        if(tmp[i]!='\0' && tmp[i]!='\t' && tmp[i]!=' ' && tmp[i]!='\n')
            break;
        if(tmp[i]=='\t'||tmp[i]==' ')
        {
            tmp[i] = '\n';    // 끝에 보통 \n가 있어서.. 추가.
            tmp[i+1]='\0';
        }
    }

    /*
    i=0;
    while(tmp[i]!='\0')
    {
        if(tmp[i]=='\0')
            printf("0");
        else if(tmp[i] == ' ')
            printf("_");
        else if(tmp[i] == '\t')
            printf("_t_");
        else
            printf("%c",tmp[i]);

        ++i;

    }*/

    //printf("%d",cnt);

    *len = strlen(tmp); // 글자 길이를 갱신한다.

//    printf("%d",*len);

    if(*len>2)
        return true;
    else
        return false;
}

'Native > C' 카테고리의 다른 글

예제 1-19  (0) 2013.10.02
예제 1-20  (0) 2013.10.02
예제 1-17  (0) 2013.10.02
예제 1-16  (0) 2013.10.02
39페이지 문자배열  (0) 2013.10.02