如何从文本字符串中删除特定字母?



我必须编写一个程序,其中我必须从文本字符串中删除特定的字母。

例如,如果用户输入像"Hepello Ipe hapeve ape prpeobpelepem"这样的文本,代码必须自动删除'pe'字母,以便文本成为正常"Hello I have a problem"

我试图用list函数来编程,但我的老师说我必须使用指针和动态结构,我很讨厌。

有人可以帮助我吗?

这是我到目前为止所拥有的。我的第二个代码仍然很空:

#include <iostream>
#include <string>
#include <list>
using namespace std;
list<string> createCodeList()
{
list<string> codeList;
string input;
cout << "Enter your coded text: " << endl;
getline(cin, input);

for (int i = 0; i < input.length(); i += 3)
{
string code = input.substr(i, 2);
codeList.push_back(code);
}
return codeList;
}
void removeCodeWords(list<string>& codeList)
{
for (auto it = codeList.begin(); it != codeList.end(); )
{
if (*it == "pe")
{
it = codeList.erase(it);
}
else
{
++it;
}
}
}
void printCodeList(const list<string>& codeList)
{
for (const string& code : codeList)
{
cout << code;
}
cout << endl;
}
int main()
{
list<string> codeList = createCodeList();
removeCodeWords(codeList);
printCodeList(codeList);
return 0;
}
#include <iostream>
#include <string>
using namespace std;
struct Codedtext
{
string text;    
};
typedef Codedtext* point;
point Createlist()
{
point list = NULL;
char input;
cout << "Enter your coded text: " << endl;
cin >> input;
}
int main()
{
return 0;
}

您的std::list代码不起作用,因为您的createCodeList()函数没有正确分解用户的输入。 它从每组 3 个字符中提取 2 个字符,无论"pe"子字符串实际位于何处(提示:它们不以 3 个字符的偶数间隔定位)。

IOW,您正在像这样分解输入:

Hep|ell|o I|pe |hap|eve| ap|e p|rpe|obp|ele|pem

Hep->He
ell->el
o I->o
pe->pe(已删除)
hap->ha
eve->ev
ap->a
e p->e
rpe->rp
obp->ob
ele->el
pem->pe(已删除)

因此,您的输出最终为:

Heelo have ae rpobel

而正确的分解看起来像这样(请注意,分组的长度不同):

He|pe|llo I|pe| ha|pe|ve a|pe| pr|pe|ob|pe|le|pe|m

He
pe(移除)
llo I
pe(移除)
ha
pe(移除)
ve a
pe(移除)
pr
pe(移除)
ob
pe(移除)
le
pe(移除)
m

所以输出将是:

您好,我有一个问题

看到区别了吗?

话虽如此,请尝试以下操作:

list<string> createCodeList()
{
list<string> codeList;
string input;
cout << "Enter your coded text: " << endl;
getline(cin, input);

string::size_type start = 0, end;
while ((end = input.find("pe", start)) != string::npos)
{
codeList.push_back(input.substr(start, end-start));
codeList.push_back("pe");
start = end + 2;
}
if (start < input.size())
codeList.push_back(input.substr(start));
return codeList;
}

现在,其余代码将按预期工作。

在线演示

话虽如此,由于您的老师不希望您使用std::list,您可以简单地自己实现链接列表,如下所示:

#include <iostream>
#include <string>
using namespace std;
struct Codedtext
{
string text;
Codedtext* next = nullptr;
};
using CodedtextPtr = Codedtext*;
CodedtextPtr createCodeList()
{
CodedtextPtr codeList = nullptr;
CodedtextPtr *node = &codeList;
string input;
cout << "Enter your coded text: " << endl;
getline(cin, input);

string::size_type start = 0, end;
while ((end = input.find("pe", start)) != string::npos)
{
*node = new Codedtext{ input.substr(start, end-start) };
node = &((*node)->next);
*node = new Codedtext{ "pe" };
node = &((*node)->next);
start = end + 2;
}
if (start < input.size())
*node = new Codedtext{ input.substr(start) };
return codeList;
}
void removeCodeWords(CodedtextPtr& codeList)
{
CodedtextPtr* previous = &codeList;

for(auto it = codeList; it != nullptr; )
{
CodedtextPtr next = it->next;
if (it->text == "pe")
{
*previous = next;
delete it;
}
else
previous = &(it->next);
it = next;
}
}
void printCodeList(CodedtextPtr codeList)
{
while (codeList)
{
cout << codeList->text;
codeList = codeList->next;
}
cout << endl;
}
void freeCodeList(CodedtextPtr codeList)
{
while (codeList)
{
CodedtextPtr next = codeList->next;
delete codeList;
codeList = next;
}
}
int main()
{
CodedtextPtr codeList = createCodeList();
removeCodeWords(codeList);
printCodeList(codeList);
freeCodeList(codeList);
return 0;
}

在线演示

最新更新