1

I'm creating a program that takes in a sentence or a paragraph. It then asks the user what they'd like to do. - Covert to all caps - Covert to all lowercase - Delete white spaces - Split words and remove duplicates - Search for a word in the string

I got the all of them but I can't figure out how to split the words and remove duplicates..

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

This is my 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;
}
maxteneff
  • 1,523
  • 12
  • 28
s.marie
  • 15
  • 5
  • 2
    First off, look up `std::tolower` and `std::toupper` to convert your characters to lower case or upper case before you compare (in the `switch` statement). For example, convert your variable `selection` before the `switch` statement. – Thomas Matthews Oct 13 '15 at 22:54
  • 2
    Hint: use `std::vector` to contain your words. You can then sort and remove duplicates using functions like `std::sort` and `std::unique`. – Thomas Matthews Oct 13 '15 at 22:57
  • Okay thank you. I will try it out – s.marie Oct 13 '15 at 22:59
  • Possible duplicate of [read word by word from file in C++](http://stackoverflow.com/questions/20372661/read-word-by-word-from-file-in-c) – bobah Oct 14 '15 at 08:10

1 Answers1

0

You can use stringstream to split your string by whitespaces and set<string> to contain only unique words. Then your code should look like:

case 'D': 
{
        cout << "You chose to split the words & remove the duplicates in the paragraph" << endl;

        string buf;
        stringstream ss(s); // Insert the string into a stream

        set<string> tokens; // Create vector to hold our words

        while (ss >> buf)
            tokens.insert(buf);

        cout << "This is it: " << endl;

        for (set<string>::iterator it = tokens.begin(); it != tokens.end(); ++it) {
            cout << *it << " ";
        }

        cout << endl;

        break;
}
maxteneff
  • 1,523
  • 12
  • 28
  • I did what you said & I am getting errors in case D. 64:21:warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 73:36: error: variable 'std::stringstream ss' has initializer but incomplete type 75:19: error: 'set' was not declared in this scope 75:29: error: expected primary-expression before '>' token 75:31: error: 'tokens' was not declared in this scope 82:41: error: missing template arguments before 'it' 82:61: error: 'it' was not declared in this scope 94:8: error: jump to case label [-fpermissive] 72:14: error: crosses initialization of 'std::string buf' – s.marie Oct 15 '15 at 21:13
  • don't forget include headers `#include ` and `#include ` – maxteneff Oct 15 '15 at 22:01