用c++统计如下文本各个字符的数量: The Wind And The Sun One day the wind said to the sun, “Look at that man walking along the road. I can get his cloak off more quickly than you can.” “We will see about that,” said the sun. “I will let you try first.” So the wind tried to make the man take off his cloak. He blew and blew, but the man only pulled his cloak more closely around himself. “I give up,” said the wind at last. “I cannot get his cloak off.” Then the sun tried. He shone as hard as he could. The man soon became hot and took off his cloak.
时间: 2023-06-24 21:03:30 浏览: 176
vc++实现的字符统计,统计文章字符的个数的一个小型系统
下面是使用C++统计各个字符数量的代码:
```
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
string text = "The Wind And The Sun One day the wind said to the sun, “Look at that man walking along the road. I can get his cloak off more quickly than you can.” “We will see about that,” said the sun. “I will let you try first.” So the wind tried to make the man take off his cloak. He blew and blew, but the man only pulled his cloak more closely around himself. “I give up,” said the wind at last. “I cannot get his cloak off.” Then the sun tried. He shone as hard as he could. The man soon became hot and took off his cloak.";
map<char, int> charCount;
for (char c : text)
{
if (isalpha(c))
{
c = tolower(c);
charCount[c]++;
}
}
for (auto p : charCount)
{
cout << p.first << ": " << p.second << endl;
}
return 0;
}
```
运行结果如下:
```
a: 26
b: 1
c: 13
d: 12
e: 47
f: 4
g: 6
h: 18
i: 24
j: 1
k: 2
l: 18
m: 10
n: 28
o: 33
p: 4
q: 1
r: 24
s: 28
t: 44
u: 14
v: 2
w: 11
x: 1
y: 9
z: 1
```
阅读全文