-5

I'm creating a program that takes in a string sentence or a paragraph. I am not quite familiar with using std:: I'm not asking for someone to do it for me, I am just wanting to see if someone can show me an example of this.

When the 4th choice (“Split Words”) is selected, the words should be put into an array or a structure and each word should be displayed with a loop. After this, duplicate removal should be performed and the program must determine the duplicate words and eliminate them. After this, the word list should be printed again.

Any help with this would be greatly appreciated. Thank you

Code: (I need help with case 'D')

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;
int main()
{

    string s;
    char selection;
    string w;

    cout << "Enter a paragraph or a sentence : " ;

    getline(cin, s);

    int sizeOfString = s.length(); 

    //cout << "The paragraph has " << sizeOfString << " characters. " << endl; ***Dummy call to see if size works. 

    //cout << "You entered " << s << endl; *** Dummy function !!

    cout << "" << endl;

    cout << "                 Menu          " << endl;
    cout <<"        ------------------------" << endl;
    cout << "" << endl;
    cout << "A -- Convert paragraph to all caps " << endl;
    cout << "B -- Convert paragraph to all lowercase " << endl;
    cout << "C -- Delete whitespaces " << endl;
    cout << "D -- Split words & remove duplicates " << endl;
    cout << "E -- Search a certain word " << endl;
    cout << "" << endl;
    cout << "Please select one of the above: " ;
    cin >> selection;
    cout << "" << endl;
    switch (selection) //Switch statement
    {
        case 'a':
        case 'A': cout << "You chose to convert the paragraph to all uppercase" << endl;
                  cout << "" << endl;
                  for(int i=0; s[i]!='\0'; i++)
                    {
                        s[i]=toupper(s[i]);
                    }
                    cout << "This is it: " << s << endl;
                  break;
        case 'b':
        case 'B': cout << "You chose to convert the paragragh to all lowercase" << endl;
                  cout << "" << endl;
                  for (int i=0; s[i] !='\0'; i++)
                  {
                      s[i]=tolower(s[i]);
                  }
                    cout << "This is it: " << s << endl;
                    break;
        case 'c':
        case 'C': cout << "You chose to delete the whitespaces in the paragraph" << endl;
                  cout << "" << endl;
                  for(int i=0; i<s.length(); i++)
                    if(s[i] == ' ') s.erase(i,1);
                  cout <<"This is it: " << s << endl;
                  break;
        case 'd':
        case 'D': cout << "You chose to split the words & remove the duplicates in the paragraph" << endl;
                  cout << "" << endl;




        case 'e':
        case 'E': cout << "You chose to search for a certain word in the paragraph. " << endl;
                  cout << "" << endl;
                  cout << "Enter the word you want to search for: ";
                  cin >> w;

                  s.find(w);
                  if ( s.find( w ) != std::string::npos )
                  {
                      cout << w << " was found in the paragraph. " << endl;

                  }
                 else 
                  {
                    cout << w << " was not found in the paragraph. " << endl;
                  }




    }


    return 0;
}
Vandal
  • 903
  • 1
  • 14
  • 27
s.marie
  • 15
  • 5
  • 2
    Possible duplicate of [How to use an array with a string](http://stackoverflow.com/questions/33113948/how-to-use-an-array-with-a-string) – Blastfurnace Oct 14 '15 at 03:51

1 Answers1

1

Write code that parses words out of the string s (Here's one method: Split a string in C++?). Print the words as you parse them and then put them in some sort of set container. A set container does not allow duplicates. Once done parsing words, iterate over the set and print the words out (there won't be duplicates).

If you need to print out the de-duped words in the order they appeared in the string, then you could keep a vector alongside the set. Like before, as you parse the words add them to the set but inspect the returned pair from the set's insert() method (it can tell you whether the inserted item was new to the set or if the insert operation was denied because the set already contained a value that was equal to the one you tried to insert: http://www.cplusplus.com/reference/set/set/insert/).

std:vector<std:string> wordVector; // Assume populated with raw parsed words
std:set<std:string> deDupedSet;
std:vector<std:string> deDupedVector;

for (int i = 0; i < wordVector.size(); i++)
{
    if (deDupedSet.insert(wordVector[i]).second())
    {
        deDupedVector.push_back(wordVector[i]);
    {
}
// Print the deDupedVector afterwards
Community
  • 1
  • 1
crchurchey
  • 188
  • 6