如何使用带有字符串的数组
How to use an array with a string
我正在创建一个程序来接收一个句子或段落。然后,它会询问用户想要做什么。-转换为全大写-转换为全小写-删除空白-拆分单词和删除重复—搜索字符串
中的单词我得到了所有的,但我不知道如何分割单词和删除重复的…
当选择第四个选项("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;
}
相关文章:
- 使用 jsoncpp 解析 json 数组字符串
- C++ 对象数组字符串输入在控制台上不起作用
- 插入了 C++ 数组字符串数据,但在显示输出时不显示
- 无法区分 JSON 对象/数组/字符串
- 为什么数组(字符串类型)的大小是 24 字节,带有单个空格元素
- 如何使用数组字符串进行输入验证
- (C++) 打印字符数组字符串的单词时出现问题
- 使用Back_inserter从任何位置的输入字符中使用Back_inserter进行过滤数组字符串
- C 函数反转字符数组字符串
- 如何在 c++ 中创建数组(字符串 [] )的向量
- 如何返回数组字符串的指针
- char数组字符串混淆
- 如何从字符数组字符串中提取空格
- 是否可以将 Bitset<8> 的值复制到数组字符串而不转换它们?
- C++数组字符串函数
- 如何在函数中操作字符数组(字符串)的指针,该指针在C / C ++中作为参数传递
- visual将音频文件存储到数组/字符串流C++中
- c++在char数组(字符串)上将what替换为
- 将数组字符串转换为intger c++(初学者)
- 需要设置数组字符串长度,使其恰好为13