-2 c++
任务:编写一个程序,读取一行文本并输出每个字母的出现次数.
#include <iostream>
#include <string>
#include <cstring>
#define N 100
using namespace std;
int main()
{
char alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
int alphacount[26];
char lot[N], *p1;
int txtlen, *p2;
cout << " Enter a line of text: " << endl;
cin.getline(lot, 99);
txtlen = strlen(lot);
p1 = lot;
p2 = &txtlen;
for (int x = 0; x < *p2; x++)
{
for (int y = 0; y < 26; y++)
{
if (*p1 == alphabet[y])
{
alphacount[y]++;
p1++;
}
}
}
cout <<;
}
Run Code Online (Sandbox Code Playgroud)
需要什么条件,用什么变量来输出字母的出现?例如:
> enter a line of text : mervlala
Run Code Online (Sandbox Code Playgroud)
输出:
a - 2,
e - 1,
l - 2,
m - 1,
r - 1,
v - 1
Run Code Online (Sandbox Code Playgroud)
你是在c ++上编程,你应该使用c ++方式.
#include <algorithm>
int main ()
{
std::string textline;
std::getline (std::cin,textline);
for(char ch = 'a'; ch <= 'z'; ch++)
{
std::size_t n = std::count(textline.begin(), textline.end(), ch);
if(n > 0)
std::cout << " - " << n << "," << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
495 次 |
| 最近记录: |