I have to code a program where I have to delete specific letters from a text string.
For example, if the user types in text like "Hepello Ipe hapeve ape prpeobpelepem"
, the code has to automatically delete the 'pe'
letters so the text becomes the normal "Hello I have a problem"
.
I tried to program this with the list
functions, but my teacher said that I HAVE to use pointers and dynamic structures, which I suck at.
Can someone help me?
Here is what I have so far. My 2nd code is still pretty empty:
#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;
}
CodePudding user response:
Your std::list
code doesn't work because your createCodeList()
function is not breaking up the user's input correctly. It is extracting 2 characters from every group of 3 characters, regardless of where the "pe"
substrings are actually located (hint: they are NOT located at even intervals of 3 characters).
IOW, you are breaking up the input like this:
Hep|ell|o I|pe |hap|eve| ap|e p|rpe|obp|ele|pem
Hep
-> He
ell
-> el
o I
-> o
pe
-> pe
(removed)
hap
-> ha
eve
-> ev
ap
-> a
e p
-> e
rpe
-> rp
obp
-> ob
ele
-> el
pem
-> pe
(removed)
So your output ends up as:
Heelo haev ae rpobel
Whereas the correct breakup would look like this instead (note the groupings are NOT the same length):
He|pe|llo I|pe| ha|pe|ve a|pe| pr|pe|ob|pe|le|pe|m
He
pe
(removed)
llo I
pe
(removed)
ha
pe
(removed)
ve a
pe
(removed)
pr
pe
(removed)
ob
pe
(removed)
le
pe
(removed)
m
So the output would be:
Hello I have a problem
See the difference?
With that said, try this instead:
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;
}
Now the rest of your code will work as expected.
That being said, since your teacher doesn't want you to use std::list
, you could simply implement the linked-list yourself, something like this:
#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;
}