In: Computer Science
The user is going to cin a whole bunch of regular text. You store each word based on its first letter. Print out all the words they used by letter. Strip all punctuation and uncapitalize everything. :
- Project set up with stl includes, correct files, and turned in correctly. (Zipped project with no inner debug folder.)
- Basic data structure made. This requires at least two connected containers. More is okay if it makes sense to you, but no way this works with just one. Be sure to comment how it works so I understand it.
- Program planned out in English in inline comments in main. Think of it as you could read this to a programmer and they could do your homework for you. No code details belong in an outline; the person you are reading it to might not use C++. Fell free to mutter to yourself. You need someone else to understand what to do. But do not just comment every line; they know how to write in whatever language they choose. Don't forget - you can use any resource here short of other people and copying code of the internet. But if you can't remember how to use cin to pull words off a sentence, look it up. In the first page of search results I see three different ways to do it. No I'm not saying what they are.
- Parsing sentence down to words works
- Putting in your data structure works
- Removing punctuation and capital letters
Input: I asked my doctor how long I had to live. She said five. I said "Five what?" She said four...
Output: A asked B C D doctor E F five five four G H how had I I I I J K L long live M my N O P Q R S she said said she said T to U V W what X Y Z
The following program can be implemented in C++ as follows:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main()
{
//creating a map of character and storing the string in a vector by the starting letter.
map<char, vector<string>> m;
// filestream variable file
fstream file;
string word, filename;
// filename of the file
filename = "file.txt";
// opening file
file.open(filename.c_str());
// extracting words from the file
while (file >> word)
{
// displaying content
m[toupper(word)].push_back(word);
}
//traversing and printing
map<char, vector<string>>:: iterator it;
for(it = m.begin(); it != m.end(); it++)
{
cout<<it->first<<" ";
for(int i = 0; i < it->second.size(); i++)
cout<<it->second[i]<<" ";
cout<<endl;
}
return 0;
}
Please upvote the answer if you find it helpful.