提取的话我是相当新的C,因此遇到比较严重的混乱与使用指针。c - 从中字符串
我试图从ASCII字符的字符串中提取的话。 例如,如果我有字符串@@ Hello..world >>,我想从字符串中获得单词“Hello”和“world”,并将它们添加到我的链接列表中。
一个字被定义为任何字母序列,并且每个字至多为64个字节。此外,函数isspace()返回一个非零值的任何字符都被认为是空白。
基本上,我使用fscanf从文件中扫描字符串,然后为每个调用我的函数的字符串read_words(char * s)从字符串中获取正确的单词并将它们添加到我的链接列表中以供进一步使用。
这里是我的代码,这似乎是扔有一个指针做一个错误。
struct node {
char *val;
struct node *next;
int count;
} *words = NULL;
void read_words(char *s)
{
struct node *tmp;
char word[64+1];
int i, check, wordStarted = 0, count = 0;
for (i = 0; s[i] != '\0'; i++)
{
if ((isspace(s[i]) != 0) || !isalpha(s[i]))
{
if (wordStarted == 1)
{
check = check_list(word);
if (check != 1) {
word[count] = '\0';
tmp = malloc(sizeof(struct node));
tmp->val = word;
tmp->count = 1;
tmp->next = words;
words = tmp;
}
count = 0;
wordStarted = 0;
}
}
else
{
word[count++] = s[i];
wordStarted = 1;
}
}
}
任何帮助,将不胜感激!
谢谢!
尝试'strtok'。它根据任意数量的拆分字符将字符串拆分为多个部分。 – chris 2012-03-09 05:44:26
您是否试过[调试](http://en.wikipedia.org/wiki/GNU_Debugger)您的代码? – Zeta 2012-03-09 05:45:23
请发表您的check_list()实现 – 2012-03-09 05:48:19