您的位置:首页 > 其它

初步了解string类型

2012-10-14 16:54 190 查看
题目:分别用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: