如何使用阵列的字符串 [英] How to use an array with a string

查看:88
本文介绍了如何使用阵列的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个程序,它发生在一个句子或段落。然后,它会询问他们想要做什么用户。
- 隐蔽到全部大写
- 隐蔽到所有小写
- 删除空格
- 分词和删除重复
- 在字符串中搜索一个字

我得到了所有的人,但我无法弄清楚如何分割的话并删除重复。

当选择了第4次的选择(分割词),则词语应该被放入一个阵列或结构,每个单词应该以循环显示。在此之后,重复除去应进行,程序必须确定重复的单词和消除它们。在此之后,单词列表应再次打印。

任何帮助,这将大大AP preciated。谢谢

这是我的code&放;我需要帮助的情况下D

 的#include<&iostream的GT;
#包括LT&;&CMATH GT;
#包括LT&; cstdlib>
#包括LT&;串GT;
#包括LT&;&算法GT;
#包括LT&;&cctype GT;使用命名空间std;
诠释主(){    字符串s;
    字符选择;
    串瓦;    COUT<< 输入段落或句子:    函数getline(CIN,S);    INT sizeOfString = s.length();    // COUT<< 该段有<< sizeOfString<< 字符。&所述;&下; ENDL; ***看到虚拟呼叫如果大小的作品。    // COUT<< 您输入<<小号所述&;&下; ENDL; ***虚拟功能!    COUT<< << ENDL;    COUT<< 菜单<< ENDL;
    COUT<< ------------------------<< ENDL;
    COUT<< << ENDL;
    COUT<< A - 转换段全部大写<< ENDL;
    COUT<< B - 转换段全部小写<< ENDL;
    COUT<< C - 删除空格<< ENDL;
    COUT<< D - 斯普利特单词放大器;删除重复项<< ENDL;
    COUT<< E - 搜索某个词<< ENDL;
    COUT<< << ENDL;    COUT<< 请选择上述之一:;
    CIN>>选择;
    COUT<< << ENDL;    开关(选择)// switch语句
    {
        案一:
        案例'A':
            COUT<< 你选择的段落转换为大写<< ENDL;
            COUT<< << ENDL;
            的for(int i = 0; S [我] ='\\ 0';!我++){
                S [i] = TOUPPER(S [I]);
            }
            COUT<< 这就是它:<<小号所述&;&下; ENDL;
            打破;
        案例'B':
        案例'B':
            COUT<< 你选择的段落转换为全部小写<< ENDL;
            COUT<< << ENDL;
            的for(int i = 0; S [我] ='\\ 0';!我++){
                S [i] = tolower的(S [I]);
            }
            COUT<< 这就是它:<<小号所述&;&下; ENDL;
            打破;
        情况下C:
        情况下C:
            COUT<< 你选择删除该段的空格<< ENDL;
            COUT<< << ENDL;
            的for(int i = 0; I< s.length();我++){
                如果(S [I] =='')
                    s.erase(I,1);
            }
            COUT<< 这就是它:<<小号所述&;&下; ENDL;
            打破;
        案D:
        案D:
            COUT<< 你选择了拆分单词放大器;删除重复的段落<< ENDL;
            COUT<< << ENDL;
        案例E:
        案例'E':
            COUT<< 你选择在段来搜索目标字。&所述;&下; ENDL;
            COUT<< << ENDL;
            COUT<< 输入你想搜索的单词:
            CIN>>瓦;            s.find(重量);
            如果(s.find(W)!=标准::字符串::非营利组织){
                COUT<< W<< 在一段被发现了。<< ENDL;
            }其他{
                COUT<< W<< 未在该段中找到。&所述;&下; ENDL;
            }
    }
    返回0;
}


解决方案

您可以使用字符串流按空格分割的字符串,设置<串&GT ; 为只包含唯一的话。那么你的code应该是这样的:

 情况下,D:
{
        COUT<< 你选择了拆分单词放大器;删除重复的段落<< ENDL;        串BUF;
        stringstream的SS(S); //将字符串转换成流        设置<串GT;令牌; //创建矢量持有我们的话        而(SS>> BUF)
            tokens.insert(BUF);        COUT<< 这就是它:<< ENDL;        对于(集<串GT;:迭代它= tokens.begin(!);它= tokens.end(); ++吧){
            COUT<< *它<< ;
        }        COUT<< ENDL;        打破;
}

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

解决方案

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

这篇关于如何使用阵列的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆