3. 输入一个字符串,删除字符串中重复的单词和标点,以这个单词第一次出现的顺序输出这个字符串。如:“where there is a will, there is a way”,输出“where there is a will way”。第一次考试,很悲剧,机子不能单步调试。。。
回去思考一下,其实很简单,算记录一下历程吧。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string GetSubStr(string pInput)
{
string pSubStr;
string pTemp;
vector<string> vec;
string::size_type n = 0;
while (n!=pInput.size()) //删除标点
{
if (pInput[n]==','||pInput[n]=='.')
{
n++;
}
else
{
pTemp += pInput[n];
n++;
}
}
string pword;
n = 0;
while (n!=pTemp.size()) //提取单词
{
if (pTemp[n]!=' ')
{
pword += pTemp[n];
n++;
}
else
{
vec.push_back(pword);
pword = "";
n++;
}
}
vec.push_back(pword);
vector<string>::iterator iter;
vector<string>::iterator iter1;
int same = 0;
for (iter=vec.begin();iter!=vec.end();++iter) //后出现的单词和先出现的单词做比较
{
for (iter1=vec.begin();iter1!=iter;++iter1)
{
if (*iter==*iter1)
{
same = 1;
break;
}
}
if (!same) //不相同则保存
{
pSubStr += *iter;
pSubStr += " ";
}
else //相同这跳过该单词
same = 0;
}
return pSubStr;
}
void main()
{
string pInputStr = "where there is a will, there is a way.";
string pOutputStr = GetSubStr(pInputStr);
cout<<pOutputStr;
}