如何从字符串中提取单词并将它们存储在c ++中的不同数组中

Posted

技术标签:

【中文标题】如何从字符串中提取单词并将它们存储在c ++中的不同数组中【英文标题】:How to extract words out of a string and store them in different array in c++ 【发布时间】:2013-11-06 12:40:16 【问题描述】:

如何拆分string并将单词存储在单独的数组中而不使用strtokistringstream并找到最大的单词?我只是一个初学者,所以我应该使用 string.h 中的基本函数来完成此操作,例如 strlenstrcpy 等。有可能这样做吗?我已经尝试过这样做,并且我正在发布我所做的事情。请纠正我的错误。

#include<iostream.h>
#include<stdio.h>
#include<string.h>
void count(char n[])

    char a[50], b[50];
    for(int i=0; n[i]!= '\0'; i++)
    
        static int j=0;
        for(j=0;n[j]!=' ';j++)
        
            a[j]=n[j];
        
        static int x=0;
        if(strlen(a)>x)
        
            strcpy(b,a);
            x=strlen(a);
        
    
    cout<<"Greatest word is:"<<b;


int main( int, char** )

    char n[100];
    gets(n);
    count(n);

【问题讨论】:

要分割字符串检查这个***.com/questions/236129/… 请注意两件事:1.) 这比 C++ 更像 C - 标签“C++”在这里令人困惑。 2.) 已被弃用 - 最好使用 并编写 'std::cout' 或 'using namespace std;'在您的程序顶部 目标数组是否应该等于源数组减去空格?因为这是一个微不足道的练习。或者它应该是一个文字字符串数组(即:char*s)? (当然,您应该使用向量和 stl 字符串,但这看起来像家庭作业,预计您会受到影响。) 【参考方案1】:

您示例中的代码看起来像是用 C 编写的。strlenstrcpy 等函数源自 C(尽管它们也是 C++ 标准库的一部分,以便通过标头 cstring 实现兼容性)。

您应该开始使用标准库学习 C++,事情会变得容易得多。如果您使用标准库中的函数,则可以使用几行代码完成拆分字符串和查找最大元素等操作,例如:

// The text
std::string text = "foo bar foobar";

// Wrap text in stream.
std::istringstream isstext;
// Read tokens from stream into vector (split at whitespace).
std::vector<std::string> wordsstd::istream_iterator<std::string>iss, std::istream_iterator<std::string>;
// Get the greatest word.
auto greatestWord = *std::max_element(std::begin(words), std::end(words), [] (const std::string& lhs, const std::string& rhs)  return lhs.size() < rhs.size(); );

编辑: 如果您真的想仅使用 std::string 中的函数深入了解细节部分,请按照以下方法将文本拆分为单词(我将找到最棒的单词留给您,这应该不会太难) :

// Use vector to store words.
std::vector<std::string> words;
std::string text = "foo bar foobar";

std::string::size_type beg = 0, end;
do 
    end = text.find(' ', beg);
    if (end == std::string::npos) 
        end = text.size();
    
    words.emplace_back(text.substr(beg, end - beg));
    beg = end + 1;
 while (beg < text.size());

【讨论】:

第一种方法,你需要这个:#include &lt;sstream&gt;【参考方案2】:

我会写两个函数。例如,第一个跳过空白字符

const char * SkipSpaces( const char *p )

    while ( *p == ' ' || *p == '\t' ) ++p;

    return ( p );

第二个复制非空白字符

const char * CopyWord( char *s1, const char *s2 )

    while ( *s2 != ' ' && *s2 != '\t' && *s2 != '\0' ) *s1++ = *s2++;
    *s1 = '\0';

    return ( s2 );

【讨论】:

注意行尾的空格。【参考方案3】:

尝试在一个小数组中获取一个单词(显然没有单词>35个字符)您可以通过检查两个连续的空格来获取该单词,然后将该数组放入 strlen() 函数中,然后检查前一个单词是否更大然后删除那个词否则保留新词 毕竟,不要忘记在每个单词捕获后使用 '\0' 或空字符初始化单词数组,否则会发生这种情况:- 假设该数组中的第一个单词是“发生”,第二个是“到”,如果您不初始化,那么您的数组将在第一次捕获之后: 发生 第二次捕获: **发生

【讨论】:

为什么单词不超过20个字符? antidisestablishmentarianismSupercalifragilisticexpialidocious。除此之外,这个答案还需要认真关注,因为我怀疑有人能理解你想要描述的内容——除此之外,我不确定我们是否还知道提问者想要达到的目标。【参考方案4】:

试试这个。这里ctr 将是句子中单个单词的数组(或向量)中的元素数。您可以通过更改main 中的函数调用来将句子从您想要的任何字母中拆分出来。

#include<iostream>
#include<string>
#include<vector>
using namespace std;
void split(string s, char ch)
    vector <string> vec;
    string tempStr;
    int ctr;
    int indexs.length();
    for(int i; i<=index; i++)
        tempStr += s[i];
        if(s[i]==ch || s[i]=='\0')
            vec.push_back(tempStr);
            ctr++;
            tempStr="";
            continue;
        
    
    for(string S: vec)
        cout<<S<<endl;

int main()
    string s;
    getline(cin, s);
    split(s, ' ');
    return 0;

【讨论】:

以上是关于如何从字符串中提取单词并将它们存储在c ++中的不同数组中的主要内容,如果未能解决你的问题,请参考以下文章

如何从多个文本框输入中读取字符串并将它们存储在 Shiny 中的向量中?

如何将文件中的不同行号存储到一个集合中?

C# 如何从存储在数据库中的 OLE 对象中保存图像

从文本文件中读取单词并存储到 C 中的动态数组 Valgrind 错误中

如何分离从表行中获取的值并将其存储在雪花中的数组中

如何将数据库中的数据提取到 javascript 文件中并将其存储在数组中?