如何在C语言中将一个字符串拆分成独立的单词并创建这些单词的数组?

qmelpv7a  于 2022-12-03  发布在  其他
关注(0)|答案(3)|浏览(206)

所以,任务如下:

  • 查找文本中首字符和尾字符相同的单词数。*

为了做到这一点,我想我首先应该拆分文本并创建单独单词的数组。
例如,字符串为:
“你好再见河狗级”
我想拆分它并得到下面的数组:

  • {“你好”,“再见”,“河”,“狗”,“水平”}*

我有拆分字符串的代码:

#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;
}

然而,它只是打印这些单词,我需要把每个单词附加到某个数组中。数组不应该是固定大小的,因为我可以根据文本的需要添加任意多的元素。如何做到这一点?

slwdgvem

slwdgvem1#

我看不出有任何理由将字符串拆分成单词。只需在迭代字符串的同时保留一个标志,以表明你是在单词的内部还是外部(即状态变量)。然后在迭代过程中保留第一个和最后一个字符的变量。当你超出一个单词或到达字符串末尾时,对它们进行比较。
一个简单的方法可能如下所示:

#include <stdio.h>

int count(const char* s)
{
    int res = 0;
    int in_word = 0;
    char first;
    char last;
    
    while(*s)
    {
        if (in_word)
        {
            if (*s == ' ')
            {
                // Found end of a word
                if (first == last) ++res;
                in_word = 0;
            }
            else
            {
                // Word continues so update last
                last = *s;
            }
        }
        else
        {
            if (*s != ' ')
            {
                // Found start of new word. Update first and last
                first = *s;
                last = *s;
                in_word = 1;
            }
        }
        ++s;
    }
    if (in_word && first == last) ++res;
    return res;
}

int main(void) 
{
    char string[100] = "hello goodbye river dog level";
    printf("found %d words\n", count(string));
    return 0;
}

输出量:

found 2 words

注意:目前的代码假设单词分隔符总是一个空格。此外,代码不处理像,.等东西。但所有这些都可以很容易地添加。

9wbgstp7

9wbgstp72#

下面是一个基于现有strtok代码的简单(但幼稚)实现。它不仅计数,而且还指出找到了哪些单词,方法是将指向它们的指针存储在一个单独的指针数组中。
这是因为strtok在原处更改了字符串,用空终止符替换了空格。

#include <stdio.h>
#include <string.h>

int main(void)
{
  char string[100] = "hello goodbye river dog level";
  char* words[10]; // this is just assuming there's not more than 10 words
  size_t count=0;

  for(char* token=strtok(string," "); token!=NULL; token=strtok(NULL, " ")) 
  {
    if( token[0] == token[strlen(token)-1] ) // strlen(token)-1 gives index of last character
    {
      words[count] = token;
      count++;
    }
  }

  printf("Found: %zu words. They are:\n", count);
  for(size_t i=0; i<count; i++)
  {
    puts(words[i]);
  }
  
  return 0;
}

输出量:

Found: 2 words. They are:
river
level
bvhaajcl

bvhaajcl3#

使用基于 Alexandria 代码的strtok。

#include <stdio.h>
#include <string.h>

int main(void)
{
    char string[] = "hello, goodbye; river, dog; level.";
    char *token = strtok(string, " ,;.");
    int counter =0;
    while( token != NULL )
    {
        if(token[0]==token[strlen(token)-1]) counter++;
        token = strtok(NULL, " ,;.");
    }
    printf("found : %d", counter);

    return 0;
}

相关问题