题目:分别用char类型、string类型从字符串“I love you!”中输出字母"o"的个数。
答案:
#include<string>
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int main()
{
char *A = new(char);
gets(A);
unsigned int B = 0,Result = 0;
for(B = 0;B < strlen(A); B++)
{
if(A[B] == 'o')
{
Result++;
}
}
cout << Result << endl;
Result = 0;
string C;
cin >> C;
for (string::size_type ix = 0;ix < C.size();ix++)
{
if (C[ix] == 'o')
{
Result++;
}
}
cout << Result << endl;
system("pause");
return 0;
}
运行结果:
I love you!
2
I love you!
2