/* 比如这个input.txt文件中的字符串: Hello world.asdf as, asd Hello every one. Let us go. 把他们去掉那些空格,标点,换行符等,读取到数组word[MAX]中。 word[0]=Hello; word[1]=world; word[2]=asdf; word[4]=as; word[5]=asd; word[6]=Hello . . . . */ #include "stdio.h" #define FILE_NAME "input.txt" #define MAX_WORD_LEN 50 #define MAX_WORD_NUM 128 #define MAX_STRING_LEN 1024 char achWord[MAX_WORD_NUM][MAX_WORD_LEN] = {0}; char achBuf[MAX_STRING_LEN] = {0}; int IsCharUpperT(char c) { return ((c >= 'A' && c <= 'Z') ? 1 : 0); } int IsCharLowerT(char c) { return ((c >= 'a' && c <= 'z') ? 1 : 0); } int main(void) { char *pStr = NULL; int nIdx = 0; int nWordIdx = 0; int nLoop = 0; int nFlag = 0; FILE *fpSrc = NULL; fpSrc = fopen(FILE_NAME, "rt"); if (NULL == fpSrc) { printf("Open file: %s error !/n", FILE_NAME); return -1; } while (fgets(achBuf, sizeof(achBuf), fpSrc)) { pStr = achBuf; printf("Read the string is: %s/n", achBuf); while (*pStr) { if (IsCharUpperT(*pStr) || IsCharLowerT(*pStr)) { achWord[nIdx][nWordIdx++] = *pStr; nFlag = 1; } else { if (nFlag) { nIdx++; nWordIdx = 0; nFlag = 0; } } pStr++; } } printf("count %d words list:/n", nIdx); nWordIdx = 0; for (nLoop = 0; nLoop < nIdx; nLoop++) { printf("/t%s/n", achWord[nLoop]); } return 0; }