C++ 如何从字符串中提取单词并将它们存储在c+中的不同数组中+;
如何拆分C++ 如何从字符串中提取单词并将它们存储在c+中的不同数组中+;,c++,string,split,C++,String,Split,如何拆分字符串并将单词存储在单独的数组中,而无需使用strok或istringstream,并找到最大的单词??我只是一个初学者,所以我应该只使用string.h中的基本函数来完成这项工作,比如strlen,strcpy等。有可能吗??我已经试着这么做了,我正在发布我所做的事情。请纠正我的错误 #include<iostream.h> #include<stdio.h> #include<string.h> void count(char n[]) {
字符串
并将单词存储在单独的数组中,而无需使用strok
或istringstream
,并找到最大的单词??我只是一个初学者,所以我应该只使用string.h中的基本函数来完成这项工作,比如strlen
,strcpy
等。有可能吗??我已经试着这么做了,我正在发布我所做的事情。请纠正我的错误
#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);
}
#包括
#包括
#包括
无效计数(字符n[])
{
字符a[50],b[50];
对于(int i=0;n[i]!='\0';i++)
{
静态int j=0;
对于(j=0;n[j]!='';j++)
{
a[j]=n[j];
}
静态int x=0;
如果(strlen(a)>x)
{
strcpy(b,a);
x=斯特伦(a);
}
}
cout我将编写两个函数。例如,第一个函数跳过空白字符
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 );
}
我会写两个函数,第一个跳过空白字符
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 );
}
尝试在一个小数组中获取一个单词(显然没有单词超过35个字符)。您可以通过检查两个连续的空格来获取单词,然后将该数组放入strlen()函数中,然后检查前一个单词是否较大,然后删除该单词,否则保留新单词
在所有这些之后,不要忘记在每个单词捕获之后使用'\0'或空字符初始化单词数组,否则会发生这种情况:-
假设数组中的第一个单词是“发生”,第二个单词是“到”,如果您不初始化,那么您的数组将位于第一个捕获之后:
发生
第二个陷阱:
*要*ppen尝试在一个小数组中获取一个单词(显然没有单词超过35个字符),您可以通过检查两个连续的空格来获取该单词,然后将该数组放入strlen()函数中,然后检查前一个单词是否较大,然后删除该单词,否则保留新单词
在所有这些之后,不要忘记在每个单词捕获之后使用'\0'或空字符初始化单词数组,否则会发生这种情况:-
假设数组中的第一个单词是“发生”,第二个单词是“到”,如果您不初始化,那么您的数组将位于第一个捕获之后:
发生
第二个陷阱:
*to pppp/ 代码中的代码看起来像是用C++编写的函数,比如代码>斯特伦< /C>和 StrucP源于C(虽然它们也是C++标准库的一部分,通过标题<代码> cSts<代码> >兼容。
<>你应该开始使用标准库学习C++,事情会变得简单很多。比如,如果你使用标准库中的函数,比如拆分字符串和找到最大元素,可以使用几行代码来完成。
// The text
std::string text = "foo bar foobar";
// Wrap text in stream.
std::istringstream iss{text};
// Read tokens from stream into vector (split at whitespace).
std::vector<std::string> words{std::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(); });
<示例>中的代码看起来像是用C编写的,例如“代码> STRLEN < /CARD>和 StrucP源于C(虽然它们也是C++标准库的一部分,通过头<代码> cSts<代码>兼容)。
<>你应该开始使用标准库学习C++,事情会变得简单很多。比如,如果你使用标准库中的函数,比如拆分字符串和找到最大元素,可以使用几行代码来完成。
// The text
std::string text = "foo bar foobar";
// Wrap text in stream.
std::istringstream iss{text};
// Read tokens from stream into vector (split at whitespace).
std::vector<std::string> words{std::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(); });
试试这个。这里的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 index{s.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;
}
#包括
#包括
#包括
使用名称空间std;
无效拆分(字符串s、字符ch){
向量向量机;
字符串tempStr;
int ctr{};
int索引{s.length()};
对于(int i{};i试试这个。这里的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 index{s.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;
}
#包括
#包括
#包括
使用名称空间std;
无效拆分(字符串s、字符ch){
向量向量机;
字符串tempStr;
int ctr{};
int索引{s.length()};
对于(int {{};II.拆分字符串),请检查此通知两个事项:1)。它比C++更为C++。标签“C++”在这里混淆。2。是否已弃用-最好改为使用并在程序顶部写上“std::cout”或“using namespace std;”目标数组是否应该等于源数组减去空格?因为这是一个简单的练习。还是应该是一个文本字符串数组(即:char*
s)?(当然,您应该使用向量和stl字符串,但这看起来像是家庭作业,您可能会受到影响。)对于拆分字符串,请检查以下两个注意事项:1。)这比C++更为复杂——标签“C++”在这里混淆了2。)更好地使用,并写“STD::CUT”或“使用命名空间STD”;在程序的顶部是目标数组,它等于源数组减去空白。因为这是一个琐碎的练习。或者它应该是一个字符串数组。(即:char*
s)?(当然,您应该使用向量和stl字符串,但这看起来像是家庭作业,您可能会遇到麻烦。)注意行尾的空格。注意行尾的空格。为什么每个词都不能超过20个字符?反疾病论
,超级生活论
。除此之外,这个答案还需要一些认真的关注,因为我怀疑是否有人能理解你想要描述的内容——除此之外,我还需要更多的关注“我不确定我们甚至还不知道发问者想要达到什么目的。为什么没有一个词超过20个字符呢?反犹太教
,超级资格主义
。除此之外,这个答案还需要一些认真的关注,因为我怀疑是否有人能理解你试图描述的内容——除此之外,我也不确定我们还不知道提问者想要达到什么目的