【发布时间】:2018-01-22 19:51:47
【问题描述】:
编辑:我确实尝试将行 arr_of_strings[arr_index_count] = first_word; 更改为 strcpy(arr_of_strings[arr_index_count], first_word); 但在打印 Word is: This 后出现分段错误
编辑 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
怎么会,word是空的,我们把
i和s放进去,word现在是isis? (与asis相同)。sentence这个词怎么会打印3次?我的算法显然有缺陷,但如果有的话,不应该将单词sentence打印 4 次(句子中的每个单词一次:This is a sentence)?
另外,我只是在学习 C,所以如果有任何其他方法可以改进算法,请告诉我。
【问题讨论】:
-
arr_of_strings是一个 char 指针数组,你将它们都指向同一个 char 数组first_word -
..完全正确。而且你没有写一个空终止符,所以“This”会被“is”->“isis”覆盖,依此类推。
-
我建议从一本书中学习;试错法不适用于 C
-
@user2719875 和
strcpy(first_word, "")你在first_word[0]只写了一个零。当您进行“手动复制”时,您不会写零。 -
“手动复制”是
first_word[index_count] = sentence[i];。 (但最糟糕的问题仍然是单字符数组。)
标签: c string function c-strings