注:转载请标明原文出处链接: https://lvxiaowen.blog.youkuaiyun.com/article/details/107193292
1 字符串数组操作函数
字符串数组操作函数有很多,如下表所示:
2 string类型
以上字符串数组操作函数多且比较复杂。C++ 标准库提供了string类类型,支持上述所有的操作,另外还增加了其他更多的功能。
3 初始化string对象的方式
4 string的常用操作
5 示例
题目描述:
1.提示用户输入姓名
2.接收用户的输入
3.然后向用户间好,hello xxx
4.告诉用户名字的长度
5.告诉用户名字的首字母是什么
6.如果用户直接入回车,那么告诉用户的输入为空
7.如果用户输入的是imooc,那么告诉用户的角色是一个管理员
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
int main(void)
{
string name;
cout << "Please enter your name: " << endl;
cin >> name;
if (name.empty())
{
cout << "Your input is empty!" << endl;
}
if (name == "imooc")
{
cout << "You are administrator" << endl;
}
cout << "hello " << name << endl;
cout << "Your name length: " << name.size() << endl;
cout << "Your name first letter: " << name[0] << endl;
system("pause");
return 0;
}
运行结果:
参考资料
[1] https://www.imooc.com/learn/381
[2] https://www.runoob.com/cplusplus/cpp-strings.html