2016년 6월 9일 목요일

이것이 c언어다. 16장 끝말잇기

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int compareLastChar(char *szSavedWord[100], char* szWordTemp,int nCount);
void printData(char *szSavedWord[100],int nCount);

int main(void)
{
    char *szSavedWord[100];
    char szWordTemp[80];
    int nCount = 0;
    int flag;

    int i;

    while(1)
    {
        printf("enter a Word (EXIT : end) : ");
        gets(szWordTemp);
        if(strcmp(szWordTemp,"end") == 0)break;

        if(nCount == 0)
        {
            szSavedWord[nCount] = (char*)malloc(sizeof(szWordTemp)+1);
            strcpy(szSavedWord[nCount],szWordTemp);
            nCount++;

        }
        else
        {
            flag = compareLastChar(szSavedWord,szWordTemp,nCount);
            if(flag == 0) // 끝말이 다르면
            {
                printf("this can't accept.\n");
            }
            else
            {
                szSavedWord[nCount] = (char*)malloc(sizeof(szWordTemp)+1);
                strcpy(szSavedWord[nCount],szWordTemp);

                for(i=0;i<nCount;i++)
                {
                    if ( strcmp(szSavedWord[i],szWordTemp) == 0) // 단어 중복검사
                    {
                        printf("this Word was entered.\n");
                        nCount--;
                    }
                }
                nCount++;
            }
        }
    }

    printData(szSavedWord,nCount);


    return 0;
}

int compareLastChar(char *szSavedWord[100], char* szWordTemp,int nCount)
{
    char chFirstch;
    char chLastch;
    int nLastWordSize;

    nLastWordSize = strlen(szSavedWord[nCount-1])-1;
    chFirstch = szWordTemp[0];
    chLastch = szSavedWord[nCount-1][nLastWordSize];

    if(chFirstch == chLastch)
        return 1;
    else
        return 0;

}
void printData(char *szSavedWord[100],int nCount)
{
    int i;
    for(i=0;i<nCount;i++)
    {
        printf("%s  ",szSavedWord[i]);
    }
    printf("\n");
}


댓글 1개:

  1. 혹시 c++ 버전은 알 수 없을까요?
    임의로 바꿔보았는데 C4996에러가 나서요 ㅠㅠ

    답글삭제