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

How to use an array with a string

本文关键字:数组 字符串 何使用      更新时间:2023-10-16

我正在创建一个程序来接收一个句子或段落。然后,它会询问用户想要做什么。-转换为全大写-转换为全小写-删除空白-拆分单词和删除重复—搜索字符串

中的单词

我得到了所有的,但我不知道如何分割单词和删除重复的…

当选择第四个选项("Split Words")时,应该将单词放入数组或结构中,并且每个单词应该以循环显示。在此之后,应该执行重复删除,程序必须确定重复的单词并消除它们。在此之后,单词列表应该再次打印。

如果有任何帮助,我将不胜感激。谢谢你

这是我的代码&我需要你帮我处理"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] != ''; 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] != ''; 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;
}

您可以使用stringstream以空格分隔字符串,使用set<string>仅包含唯一的单词。那么你的代码应该看起来像:

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;
}