2016-03-05 42 views
-1

我在C初學者++和我犯了一個代碼提到了字符的字符串多少次存在: -如何自動查找一個字符在一個字符串中存在多少次?

#include <string> 
#include <iostream> 
using namespace std; 
int main(){ 
int s=0; 
int l=0; 
int o=0; 
int w=0; 
int y=0; 
string boo="slowly"; 
for (size_t j = 0; j < boo.size(); j++) { 
while (boo[j] == 's') { 
s=s+1; 
break;} 
while (boo[j] == 'l') 
{ 
l=l+1; 
break;} 
while (boo[j] == 'o') { 
o=o+1; 
break;} 
while (boo[j] == 'w') { 
w=w+1; 
break;} 
while (boo[j] == 'y') { 
y=y+1; 
break;}} 

cout <<"s ="<<s<<endl; 
cout <<"l ="<<l<<endl; 
cout <<"o ="<<o<<endl; 
cout <<"w ="<<w<<endl; 
cout <<"y="<<y<<endl; 
system("pause"); 
return 0; 
} 

我想知道如何使一個代碼,自動檢測字符串中的字符和對其應用條件而不爲每個單字母字符製作一個while循環併爲每個字符設置int變量?

**請原諒我糟糕Englihs

+1

的可能的複製[計數字符串中的字符出現](HTTP://計算器。com/questions/3867890/count-character-occurrences-in-a-string) – LogicStuff

+1

No sir我不想要確切的字符我想自動檢測字符串中的每個字符並提及每個字符重複的次數 – Dreamer

回答

1

您可以使用地圖來跟蹤不同的字符數。你迭代輸入一次,併爲每個角色在地圖上查找它。如果地圖中尚不存在,則會創建一個值爲0的新條目。

創建地圖後,每個條目都以字符作爲關鍵字,出現次數爲值,您遍歷地圖並打印每個條目。

#include <iostream> 
#include <map> 
#include <string> 

int main() 
{ 
    std::string input = "slowly"; 

    std::map<char, int> occurrences; 

    for (char character : input) 
    { 
     occurrences[character] += 1; 
    } 

    for (auto& entry : occurrences) 
    { 
     std::cout << entry.first << '=' << entry.second << std::endl; 
    } 
} 
+0

它不起作用,它爲(自動字符:輸入)的這一行提供了錯誤 – Dreamer

+0

您需要確保通過在編譯器中使用標誌'-std = C++ 11'來編譯C++ 11。 – user764486

+0

你可以在這裏看到一個演示:http://ideone.com/0GdzN0 – user764486

0

使用一個小竅門。首先,觀察到所有小寫字符以升序與數字代碼編碼:

 ASCII EBCDIC 
'a' - 97 129 
'b' - 98 130 
'c' - 99 131 
... 
'z' - 122 169 

這些代碼可以不是連續的,如在EBCDIC的情況下,但並不重要:使一個int陣列的足夠的大小,遍歷您的字符串,併爲每個小寫字符ch標記索引ch-'a'。表達式表示字符代碼和字母表開頭之間的距離。

現在你可以走的int在數組,並通過執行指數i的逆翻譯打印相應的字符:

char ch = 'a'+i; 

下面是一個完整的例子:

string s; 
getline(cin, s); 
int seen['z'-'a'+1] {0}; 
for (int i = 0 ; i != s.size() ; i++) { 
    char ch = s[i]; 
    if (!islower(ch)) 
     continue; 
    seen[ch-'a']++; 
} 
for (int i = 0 ; i != 'z'-'a'+1 ; i++) { 
    if (seen[i]) { 
     cout << (char)('a'+i) << " - " << seen[i] << endl; 
    } 
} 

Demo.

+0

這很有幫助謝謝 – Dreamer

0

試試這個:

#include <string> 
#include <iostream> 
using namespace std; 
int main(){ 
    int s=0,l=0,o=0,w=0,y=0; 
    string boo="slowly"; 
    for (int j = 0; j < boo.size(); j++) { 
     switch(boo.charAt(j)){ 
      case 's': 
       s++; 
       break; 
      case 'l': 
       l++; 
       break; 
      case 'o': 
       o++; 
       break; 
      case 'w': 
       w++; 
       break; 
      case 'y': 
       y++; 
       break; 
     } 
    } 
    cout <<"s ="<<s<<endl; 
    cout <<"l ="<<l<<endl; 
    cout <<"o ="<<o<<endl; 
    cout <<"w ="<<w<<endl; 
    cout <<"y="<<y<<endl; 
    system("pause"); 
    return 0; 
} 
+0

charAt行中的錯誤 – Dreamer

+0

對不起,它在():switch(boo.at(j)){:讓我們換到這個代碼。 charAt是java函數。 C++函數在。 –

0

如果你的編譯器支持C++ 2011,那麼你可以寫下面的方式提供的信件將在其存在的串

#include <iostream> 
#include <string> 
#include <map> 

int main() 
{ 
    std::string s("slowly"); 
    auto comp = [&](char c1, char c2) { return s.find(c1) < s.find(c2); }; 
    std::map<char, int, decltype(comp)> m(comp); 

    for (char c : s) ++m[c]; 

    for (auto p : m) std::cout << p.first << ": " << p.second << std::endl; 
}  

在訂單中列出的程序輸出是

s: 1 
l: 2 
o: 1 
w: 1 
y: 1 

否則,如果編譯器不支持C++ 2011,那麼代替lambda必須使用在main之前定義的函數類。你也應該用基於範圍的循環代替普通的循環。

0

我猜STL的算法頭將幫助您:

std::string boo="slowly"; 
std::cout << "s = " << std::count(boo.cbegin(), boo.cend(), 's') << std::endl; 
相關問題