題目分析:
注意空格也算作字符統計,使用getline函數接收字符串。
源代碼:
[cpp]
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
string c;
while (getline(cin, c))
{
if (c[0] == '#')
{
break;
}
string s;
getline(cin, s);
for (int i = 0; i < c.length(); i++)
{
int n = 0;
for (int j = 0; j < s.length(); j++)
{
if (c[i] == s[j])
{
n++;
}
}
cout<<c[i]<<" "<<n<<endl;
}
}
return 0;
}
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
string c;
while (getline(cin, c))
{
if (c[0] == '#')
{
break;
}
string s;
getline(cin, s);
for (int i = 0; i < c.length(); i++)
{
int n = 0;
for (int j = 0; j < s.length(); j++)
{
if (c[i] == s[j])
{
n++;
}
}
cout<<c[i]<<" "<<n<<endl;
}
}
return 0;
}