编辑:我曾尝试改变线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