String 如何从cout更改字符串的值

String 如何从cout更改字符串的值,string,String,我必须编写一个函数来提取'*'之间的单词。 例如,使用以下三个测试用例: string s=“这是要*颠倒的*” string s1=“*反向*从这里开始” string s2=“这是*在*中间” 在每次函数调用之后, s=reversed,s1=reversed,s2=in 所以我想 void extractWord (string& str) void提取字(字符串和str) { char target='*'; int-idx1=0; while(str[idx1]!=目标) i

我必须编写一个函数来提取'*'之间的单词。 例如,使用以下三个测试用例:

string s=“这是要*颠倒的*”

string s1=“*反向*从这里开始”

string s2=“这是*在*中间”

在每次函数调用之后,
s=reversed,s1=reversed,s2=in

所以我想

void extractWord (string& str)
void提取字(字符串和str)
{
char target='*';
int-idx1=0;
while(str[idx1]!=目标)
idx1=idx1+1;
int idx2=idx1+1;
while(str[idx2]!=目标)
idx2=idx2+1;

对于(int i=0;iidx1)和&(i我对您的代码进行了一些修改。我希望这能解决您的问题:

void extractWord (string& str)

{
char target= '*';

int idx1=0;

while (str[idx1] != target)
idx1=idx1+1;

int idx2=idx1+1;
while (str[idx2] != target)
idx2=idx2+1;

for(int i=0;i<sizeof(str);i++)
     {
     if ((i>idx1)&&(i<idx2))
     cout<<str[i];
     }
}


int main()
{
string s="This is to be *reversed*";
string s1 = "*Reversed* starts here";
string s2= "This is *in* the middle";

extractWord(s);
cout<<endl;
extractWord(s1);
cout<<endl;
extractWord(s2);
cout<<endl;
}
/#包括“stdafx.h”
#包括
#包括
使用名称空间std;
void提取字(字符串和str)
{
char target='*';
int-idx1=0;
while(str[idx1]!=目标)
idx1=idx1+1;
int idx2=idx1+1;
while(str[idx2]!=目标)
idx2=idx2+1;
str=str.substr(idx1+1,idx2-idx1-1);//已更改
}
int main()
{
string s=“这是要*反转*”;
字符串s1=“*反向*从此处开始”;
string s2=“这是*在*中间”;
抽取字;
库特
//#include "stdafx.h"
#include < string >
#include < iostream >
using namespace std;

void extractWord (string& str)

{
char target= '*';

int idx1=0;

while (str[idx1] != target)
idx1=idx1+1;

int idx2=idx1+1;
while (str[idx2] != target)
idx2=idx2+1;

str=str.substr(idx1+1,idx2-idx1-1); //changed 
}


int main()
{
string s="This is to be *reversed*";
string s1 = "*Reversed* starts here";
string s2= "This is *in* the middle";

extractWord(s);
cout<<s<<endl; //changed 
extractWord(s1);
cout<<s1<<endl; //changed 
extractWord(s2);
cout<<s2<<endl; //changed 

    system("PAUSE");
    return 0;
}
//#include "stdafx.h"
#include < string >
#include < iostream >
using namespace std;

string extractWord (string& str)

{
char target= '*';

int idx1=0;

while (str[idx1] != target)
idx1=idx1+1;

int idx2=idx1+1;
while (str[idx2] != target)
idx2=idx2+1;

return str.substr(idx1+1,idx2-idx1-1);
}


int main()
{
string s="This is to be *reversed*";
string s1 = "*Reversed* starts here";
string s2= "This is *in* the middle";
string res;
res=extractWord(s);
cout<<res<<endl;
res=extractWord(s1);
cout<<res<<endl;
res=extractWord(s2);
cout<<res<<endl;

    system("PAUSE");
    return 0;
}