简体   繁体   English

如何使用带有字符串的数组

[英]How to use an array with a string

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' 这是我的代码,我需要案例'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;
}

You can use stringstream to split your string by whitespaces and set<string> to contain only unique words. 您可以使用stringstream用空格分隔字符串,并将set<string>为仅包含唯一单词。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM