編輯:我曾嘗試改變線arr_of_strings[arr_index_count] = first_word;
到strcpy(arr_of_strings[arr_index_count], first_word);
但隨後它給打印Word is: This
字符串無法正常被清空,並分配了的strcpy打交道時(字符串,「」)
編輯2後分段錯誤:我我試圖做到這一點沒有strtok
,因爲我認爲這將是瞭解C字符串的好方法。
試圖學習C我自己。決定創建一個接受字符串的函數,並將字符串中的每個單詞放入數組中的一個元素中。這裏是我的代碼:
假設#define MAX_LENGTH = 80
// char *string_one[unknown_size];
// first_word will represent each word in the sentence
char first_word[MAX_LENGTH + 1] = "";
// this is the array I will store each word in
char *arr_of_strings[MAX_LENGTH];
int index_count = 0;
int arr_index_count = 0;
char sentence[] = "This is a sentence.";
for (int i = 0; i<MAX_LENGTH; i++) {
printf("Dealing with char: %c\n", sentence[i]);
if (sentence[i] == '\0') {
// end of sentence
break;
} else if (sentence[i] == ' ') {
// this signifies the end of a word
printf("Word is: %s\n", first_word);
arr_of_strings[arr_index_count] = first_word;
// after putting the word in the string, make the word empty again
strcpy(first_word, "");
// verify that it is empty
printf("First word is now: %s\n", first_word);
index_count = 0;
arr_index_count++;
} else {
// not the start of a new string... so keep appending the letter to first_word
printf("Letter to put in first_word is: %c\n", sentence[i]);
first_word[index_count] = sentence[i];
index_count++;
}
}
printf("-----------------\n");
for (int j = 0; j<=arr_index_count; j++) {
printf("%s\n", arr_of_strings[j]);
}
這是什麼版畫是:
Dealing with char: T
Letter to put in first_word is: T
Dealing with char: h
Letter to put in first_word is: h
Dealing with char: i
Letter to put in first_word is: i
Dealing with char: s
Letter to put in first_word is: s
Dealing with char:
Word is: This
First word is now:
Dealing with char: i
Letter to put in first_word is: i
Dealing with char: s
Letter to put in first_word is: s
Dealing with char:
Word is: isis
First word is now:
Dealing with char: a
Letter to put in first_word is: a
Dealing with char:
Word is: asis
First word is now:
Dealing with char: s
Letter to put in first_word is: s
Dealing with char: e
Letter to put in first_word is: e
Dealing with char: n
Letter to put in first_word is: n
Dealing with char: t
Letter to put in first_word is: t
Dealing with char: e
Letter to put in first_word is: e
Dealing with char: n
Letter to put in first_word is: n
Dealing with char: c
Letter to put in first_word is: c
Dealing with char: e
Letter to put in first_word is: e
Dealing with char: .
Letter to put in first_word is: .
Dealing with char:
-----------------
sentence.
sentence.
sentence.
如果我們看看這裏:
First word is now:
Dealing with char: i
Letter to put in first_word is: i
Dealing with char: s
Letter to put in first_word is: s
Dealing with char:
Word is: isis
怎麼來的,當字是空的,我們把
i
和s
進去吧,單詞現在是isis
? (與asis
相同)。爲什麼字
sentence
被打印3次?我的算法顯然是有缺陷的,但是如果有的話,不應該打印4次(對於句子中的每個單詞一次:這是一個句子)這個詞sentence
?
此外,我只是學習C所以,如果有任何其他的方法來改進算法,請讓我知道。
'arr_of_strings'是char指針數組,而你點他們都在同一字符數組'first_word' –
..完全。而且你不寫一個null結束符,所以「this」被「is」 - >「isis」覆蓋,依此類推。 – alain
@ M.M我試圖將該行更改爲'strcpy(arr_of_strings [arr_index_count],first_word);'但是它在打印後出現分段錯誤'Word is:This' – user2719875