我正在創建一個程序,需要一個句子或一個段落。然後詢問用戶他們想要做什麼。 - 隱蔽到全部大寫 - 隱蔽到所有小寫 - 刪除空格 - 斯普利特字和刪除重複 - 在字符串中搜索詞語如何與字符串一起使用數組
我得到了所有的人,但我想不出如何拆分單詞並刪除重複內容。
當選擇第4個選項(「拆分詞」)時,應該將單詞放入數組或結構中,並且每個單詞應該用循環顯示。之後,應該執行重複刪除操作,程序必須確定重複的文字並將其刪除。在此之後,單詞列表應該再次打印。
任何幫助,這將不勝感激。謝謝
這是我的代碼&我需要的情況下「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 :: tolower'和'的std :: toupper'給你的角色你比較之前轉換爲小寫或大寫(在'之開關聲明)。例如,在'switch'語句之前轉換變量'selection'。 –
提示:使用'std :: vector'來包含你的單詞。然後你可以使用'std :: sort'和'std :: unique'等函數來排序和刪除重複項。 –
好的,謝謝。我會試試 –