0

我正在解析一个文本文件:

Hello, this is a text file.

并通过将文件转换为 char[] 来创建。现在我想获取数组,遍历它,并创建一个数组数组,将文件拆分为单词:

 string[0] = Hello
 string[1] = this
 string[2] = is

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "TextReader.h"
#include <ctype.h>

void printWord(char *string) {
int i;
for (i = 0; i < strlen(string); i ++)
    printf("%c", string[i]);
printf("\n");
}

void getWord(char *string) {
char sentences[5][4];
int i;
int letter_counter = 0;
int word_counter = 0;

    for (i = 0; i < strlen(string); i ++) {
            // Checks if the character is a letter
    if (isalpha(string[i])) {
        sentences[word_counter][letter_counter] = string[i];
        letter_counter++;
    } else {
        sentences[word_counter][letter_counter + 1] = '\0';
        word_counter++;
        letter_counter = 0;
    }
}

// This is the code to see what it returns:
i = 0;
for (i; i < 5; i ++) {
    int a = 0;
    for (a; a < 4; a++) {
        printf("%c", sentences[i][a]);
    }
    printf("\n");
}
}

int main() {
    // This just returns the character array. No errors or problems here.
char *string = readFile("test.txt");

getWord(string);

return 0;
}

这是它返回的内容:

Hell
o
this
is
a) w

我怀疑这与指针和东西有关。我来自强大的 Java 背景,所以我仍然习惯于 C。

4

2 回答 2

3

随着sentences[5][4]您将数量限制sentences为 5,每个单词的长度限制为 4。您需要将其变大以处理更多和更长的单词。试试sentences[10][10]。您也没有检查您输入的单词是否不超过sentences可以处理的长度。使用更大的输入,这可能导致堆溢出和访问冲突,请记住 C 不会为您检查指针!

当然,如果你打算用这个方法来处理更大的文件和更大的单词,你需要把它变大或者动态分配它

于 2013-04-15T23:43:38.987 回答
0

不使用 strtok 的示例:

void getWord(char *string){
    char buff[32];
    int letter_counter = 0;
    int word_counter = 0;
    int i=0;
    char ch;

    while(!isalpha(string[i]))++i;//skip
    while(ch=string[i]){
        if(isalpha(ch)){
            buff[letter_counter++] = ch;
            ++i;
        } else {
            buff[letter_counter] = '\0';
            printf("string[%d] = %s\n", word_counter++, buff);//copy to dynamic allocate array
            letter_counter = 0;
            while(string[++i] && !isalpha(string[i]));//skip
        }
    }
}

使用 strtok 版本:

void getWord(const char *string){
    char buff[1024];//Unnecessary if possible change
    char *p;
    int word_counter = 0;

    strcpy(buff, string);
    for(p=buff;NULL!=(p=strtok(p, " ,."));p=NULL){//delimiter != (not isaplha(ch))
        printf("string[%d] = %s\n", word_counter++, p);//copy to dynamic allocate array
    }
}
于 2013-04-16T09:28:29.880 回答