2015-10-13 36 views
1

我正在創建一個程序,需要一個句子或一個段落。然後詢問用戶他們想要做什麼。 - 隱蔽到全部大寫 - 隱蔽到所有小寫 - 刪除空格 - 斯普利特字和刪除重複 - 在字符串中搜索詞語如何與字符串一起使用數組

我得到了所有的人,但我想不出如何拆分單詞並刪除重複內容。

當選擇第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; 
} 
+2

首先,擡頭'的std :: tolower'和'的std :: toupper'給你的角色你比較之前轉換爲小寫或大寫(在'之開關聲明)。例如,在'switch'語句之前轉換變量'selection'。 –

+2

提示:使用'std :: vector '來包含你的單詞。然後你可以使用'std :: sort'和'std :: unique'等函數來排序和刪除重複項。 –

+0

好的,謝謝。我會試試 –

回答

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

I D:64:21:警告:有符號和無符號整數表達式之間的比較[-Wsign-compare] 73:36:錯誤:變量'std :: stringstream ss'有初始值設定項但是不完整類型 75:19:錯誤:'set'未在此範圍內聲明 75:29:錯誤:預期的主表達式在'>'標記之前 75:31:錯誤:'標記'未在此範圍內聲明 82:41錯誤:在'它'之前缺少模板參數 82:61:錯誤:'它'未在此範圍內聲明 94:8:錯誤:跳轉到案例標籤[-fpermissive] 72:14:錯誤:交叉初始化'std :: string buf' –

+0

不要忘記包含頭文件'#include '和'#include ' – maxteneff

相關問題