所以,任务如下:
- 查找文本中首字符和尾字符相同的单词数。*
为了做到这一点,我想我首先应该拆分文本并创建单独单词的数组。
例如,字符串为:
“你好再见河狗级”
我想拆分它并得到下面的数组:
- {“你好”,“再见”,“河”,“狗”,“水平”}*
我有拆分字符串的代码:
#include<stdio.h>
#include <string.h>
int main() {
char string[100] = "hello goodbye river dog level";
// Extract the first token
char * token = strtok(string, " ");
// loop through the string to extract all other tokens
while( token != NULL ) {
printf( " %s\n", token ); //printing each token
token = strtok(NULL, " ");
}
return 0;
}
然而,它只是打印这些单词,我需要把每个单词附加到某个数组中。数组不应该是固定大小的,因为我可以根据文本的需要添加任意多的元素。如何做到这一点?
3条答案
按热度按时间slwdgvem1#
我看不出有任何理由将字符串拆分成单词。只需在迭代字符串的同时保留一个标志,以表明你是在单词的内部还是外部(即状态变量)。然后在迭代过程中保留第一个和最后一个字符的变量。当你超出一个单词或到达字符串末尾时,对它们进行比较。
一个简单的方法可能如下所示:
输出量:
注意:目前的代码假设单词分隔符总是一个空格。此外,代码不处理像
,
.
等东西。但所有这些都可以很容易地添加。9wbgstp72#
下面是一个基于现有
strtok
代码的简单(但幼稚)实现。它不仅计数,而且还指出找到了哪些单词,方法是将指向它们的指针存储在一个单独的指针数组中。这是因为
strtok
在原处更改了字符串,用空终止符替换了空格。输出量:
bvhaajcl3#
使用基于 Alexandria 代码的strtok。