这是 Ansi C 语言。我得到了一个字符串。我应该创建一个方法,返回一个字符指针数组,该数组指向所述字符串的每个单词的开头。我不被允许使用Malloc,而是被告知输入的最大长度为80。
另外,在有人因为我没有搜索论坛而责备我之前,我不能使用 strtok :(
char input[80] = "hello world, please tokenize this string"
该方法的输出应有 6 个元素;
output[0] points to the "h",
output[1] points to the "w",
等等。
我应该如何编写该方法?
此外,我需要一个类似的方法来处理最多 110 行的文件的输入。
最佳答案
伪代码:
boolean isInWord = false
while (*ptr != NUL character) {
if (!isInWord and isWordCharacter(*ptr)) {
isInWord = true
save ptr
} else if (isInWord and !isWordCharacter(*ptr)) {
isInWord = false
}
increment ptr
}
isWordCharacter
检查该字符是否是单词的一部分。根据您的定义,它只能是字母字符(将 part-time
识别为 2 个单词),也可能包括 -
(将 part-time
识别为一个单词)。
关于c - 在 ANSI C 中使用指针标记字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11150092/