2016년 6월 12일 일요일

이것이 c언어다. 14장 문자열 검색

//문자열을 입력하고, 문자열을 검색하여 중복을 확인함.
//근데..... szSavedVoca[0]에는  자꾸 이상한값이 들어가냐...;;
#include <stdio.h>
#include <string.h>
int main(void)
{
    char szSavedVoca[10][20] ;
    char pTempWord [20];
    int nCount = 0;
    int i;
    while(1)
    {
        printf("enter the Word (EXIT end) : ");
        gets(pTempWord);            //pTempWord  저장후 문자열 배열에 저장하는 이유는
                                    // 바로 문자열에 저장하게 되면 사용자가 10개 이상의 문자열입력시
                                    // 문자열 크기를 넘어가서 저장하게됨.
        if(strcmp(pTempWord, "end") == 0)
            break;
        else if(nCount >= 10)
            break;
        else
        {
            strcpy(szSavedVoca[nCount],pTempWord);
            for(i=0;i<nCount;i++)
                {
                    if(nCount == 0) break;
                    else if (strcmp(szSavedVoca[nCount],szSavedVoca[i]) == 0)
                    {
                        printf("this Word was entered. to %d\n",i+1);
                        nCount--;
                    }
                }
        }
        strcpy(pTempWord,""); //pTempWord 초기화.
        nCount++;
    }
    int flag = 1;
    printf("# total Words of %d are entered.\n",nCount);
    printf("enter the searching Word : ");
    while(strcmp(gets(pTempWord), "end") != 0)
    {
        for(i=0;i<=nCount;i++)
        {
            if (strcmp(szSavedVoca[i],pTempWord) == 0)
            {
                printf("this Word existed to %d\n",i+1);
                printf("enter the searching Word : ");
                flag = 0;
            }
        }
        if(flag == 1)
        {
            printf("this Word not exist\n");
            printf("enter the searching Word : ");
        }
        flag = 1;
    }
    return 0;
}


댓글 없음:

댓글 쓰기