Visual Studio Developer: C++ 1) write a function called fill_list that takes par
ID: 3779425 • Letter: V
Question
Visual Studio Developer: C++
1) write a function called fill_list that takes parameters for a list and the string to add. The function should iterate through the string and add each to the list. (So a string "ABC" should be inserted into a list which will consist of elements [A]->[B]->[C])
2) write a function called removeCharacter that takes parameters for the list and a character to move. It should iterat through the list, moving each instance of the charater. Think about what happens when you remove characters ina list, and what different types of conditions might occur after doing this.
3) write a function called printList which takes in a lost and an ostream (output stream). It should iterate through a list an print out each element, showing the order they are connected.(From step 1 above, it should print out like "[A] -> [B] -> [C] -> 0", where 0 is the NULL pointer at the end.)
4) in the main, read in input.txt (already provided) on line at a time. It should stop when it gets to the last word on the line. Call fill_list to fill the list with the line from the file. After that, make calls to removeCharacter to remove all VOWELS. Then call printList to print it out to a file called output.txt
output.txt
input.txt
Explanation / Answer
#include <iostream> //For cin cout
#include <fstream> //Input output stream
#include <string> //For strings
#include <list> //For list types
using namespace std;
//Fill list function prototype
void fillList(list<char>& lstVectorParam, string inputStringParam);
//Function to print the list
void printList(list<char>& lstParam, ostream& outputStream);
//Remove character function
void removeCharacter(list<char>& lstParam, char& charParam);
int main()
{
ifstream fin("input.txt"); //Link the input file
ofstream fout("output.txt"); //Link to an output file
list<char> strList; //A char list type
string lineFromFile; //Variable to hold the data from the input file
//List of vowels to remove from the list
char vowels[] = {'a', 'A', 'E','e', 'I','i','O','o','U','u' };
while(!fin.eof()) //run unit the end of hte file
{
getline(fin,lineFromFile); //get a line of text
if(lineFromFile == "END")
break;
fillList(strList,lineFromFile); //fill strList with a line of text
for(int i = 0; i < 10; i++) //Check for each vowel in the list
removeCharacter(strList,vowels[i]); //Remove vowel function
printList(strList,fout); //Write the list to a file
strList.clear(); //Clear the current list
}
return 0;
}
void fillList(list<char>& lstParam, string inputStringParam)
{
for(int i = 0; i< inputStringParam.size(); i++)
{
lstParam.push_back(inputStringParam[i]);
}
}
void printList(list<char>& lstParam, ostream& outputStream)
{
for( list<char>::iterator iter = lstParam.begin(); iter != lstParam.end(); iter++)
outputStream << "[" <<*iter<<"]" <<" -> ";
outputStream << " 0 " << " " << endl;
}
void removeCharacter(list<char>& lstParam, char& charParam)
{
for( list<char>::iterator iter = lstParam.begin(); iter != lstParam.end();)
{
if(*iter == charParam)
iter = lstParam.erase(iter);
else
iter++;
}
}
input.txt
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
Bagel
Cupcake
Donut
Eclair
Froyo
Gingerbread
Honeycomb
Ice cream sandwich
Jellybean
Kit Kat
The quick brown fox jumps over the lazy dog.
I before E except after C
BBBBBBBBBBbbbbbbbbbbaa
CCCCCCCCCCcccccccccc
AeIoU
END
output.txt
[B] -> [C] -> [D] -> [F] -> [G] -> [H] -> [J] -> [K] -> [L] -> [M] -> [N] -> [P] -> [Q] -> [R] -> [S] -> [T] -> [V] -> [W] -> [X] -> [Y] -> [Z] -> 0
[b] -> [c] -> [d] -> [f] -> [g] -> [h] -> [j] -> [k] -> [l] -> [m] -> [n] -> [p] -> [q] -> [r] -> [s] -> [t] -> [v] -> [w] -> [x] -> [y] -> [z] -> 0
[B] -> [g] -> [l] -> 0
[C] -> [p] -> [c] -> [k] -> 0
[D] -> [n] -> [t] -> 0
[c] -> [l] -> [r] -> 0
[F] -> [r] -> [y] -> 0
[G] -> [n] -> [g] -> [r] -> [b] -> [r] -> [d] -> 0
[H] -> [n] -> [y] -> [c] -> [m] -> [b] -> 0
[c] -> [ ] -> [c] -> [r] -> [m] -> [ ] -> [s] -> [n] -> [d] -> [w] -> [c] -> [h] -> 0
[J] -> [l] -> [l] -> [y] -> [b] -> [n] -> 0
[K] -> [t] -> [ ] -> [K] -> [t] -> 0
[T] -> [h] -> [ ] -> [q] -> [c] -> [k] -> [ ] -> [b] -> [r] -> [w] -> [n] -> [ ] -> [f] -> [x] -> [ ] -> [j] -> [m] -> [p] -> [s] -> [ ] -> [v] -> [r] -> [ ] -> [t] -> [h] -> [ ] -> [l] -> [z] -> [y] -> [ ] -> [d] -> [g] -> [.] -> 0
[ ] -> [b] -> [f] -> [r] -> [ ] -> [ ] -> [x] -> [c] -> [p] -> [t] -> [ ] -> [f] -> [t] -> [r] -> [ ] -> [C] -> [ ] -> 0
[B] -> [B] -> [B] -> [B] -> [B] -> [B] -> [B] -> [B] -> [B] -> [B] -> [b] -> [b] -> [b] -> [b] -> [b] -> [b] -> [b] -> [b] -> [b] -> [b] -> 0
[C] -> [C] -> [C] -> [C] -> [C] -> [C] -> [C] -> [C] -> [C] -> [C] -> [c] -> [c] -> [c] -> [c] -> [c] -> [c] -> [c] -> [c] -> [c] -> [c] -> 0
0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.