初步了解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
答案:
#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
相关文章推荐
- 0918string的操作和json的初步了解~
- JAVA新手入门--对数据类型/标识符/包等概念的初步认识与了解
- 0708学习笔记初步了解函数作用域和补充非可变类型参数的函数与可变类型参数的函数
- mysql初步认识,了解mysql中的基本数据类型
- 由String的内存分配了解Object和基础数据类型内存分配
- 学习的枚举类型,结构以及初步了解数组
- 将C++的std::string类型变量赋值给char类型数组
- string类型的数字化处理
- Java int 与 string类型相互转换
- 1-3 ZigBee协议栈的初步了解
- [php] pdo 查询 mysql 返回值 int 转 string 类型的问题
- 如何将C++中的string 类型转换成 char*
- JAVA中int、String的类型转换
- 用模板template<typename T>实现int double等类型转string
- 字符串(string)类型
- 黑马程序员——43,String,StringBuffer ,基本数据类型包装对象
- C++中将string类型转换为int, float, double类型 主要通过以下几种方式:
- QT中string和其他类型之间的转换
- 初步了解正则表达式
- string 类型