【问题标题】:How to use an array with a string如何将数组与字符串一起使用
【发布时间】:2015-10-13 22:53:44
【问题描述】:

我正在创建一个接收句子或段落的程序。然后它会询问用户他们想做什么。 - 隐蔽到所有大写 - 隐藏到所有小写字母 - 删除空格 - 拆分单词并删除重复项 - 在字符串中搜索一个单词

我得到了所有这些,但我不知道如何拆分单词并删除重复项..

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

对此的任何帮助将不胜感激。谢谢

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

【问题讨论】:

  • 首先,在比较之前查找std::tolowerstd::toupper 以将字符转换为小写或大写(在switch 语句中)。例如,在switch 语句之前转换您的变量selection
  • 提示:使用std::vector&lt;std::string&gt; 包含您的文字。然后,您可以使用 std::sortstd::unique 等函数对重复项进行排序和删除。
  • 好的,谢谢。我试试看

标签: c++ arrays string


【解决方案1】:

您可以使用stringstream 以空格分隔字符串,并使用set&lt;string&gt; 以仅包含唯一单词。那么你的代码应该是这样的:

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

【讨论】:

  • 我按照你说的做了,在情况 D. 64:21:warning: 有符号和无符号整数表达式之间的比较 [-Wsign-compare] 73:36: 错误:变量 'std ::stringstream ss' 具有初始化程序但类型不完整 75:19:错误:未在此范围内声明“set” 75:​​29:错误:“>”令牌之前的预期主表达式 75:31:错误:“令牌”是未在此范围内声明 82:41:错误:在“it”之前缺少模板参数 82:61:错误:“it”未在此范围内声明 94:8:错误:跳转到案例标签 [-fpermissive] 72:14 : 错误: 跨越 'std::string buf' 的初始化
  • 不要忘记包含标题 #include &lt;sstream&gt;#include &lt;set&gt;
猜你喜欢
  • 2016-04-24
  • 2017-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 2021-02-16
  • 1970-01-01
相关资源
最近更新 更多