提取的話我是相當新的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