字符数组
字符数组
我们先来看一个简单的例子:
声明并初始化一个字符数组
char arr[] = {'h', 'e', 'l', 'l', 'o'};
char arr2[] = "hello";
这两种写法虽然不一样,不过存储方式是一样的。
#include <iostream>
using namespace std;
void printArr(char a[], int n){
for(int i = 0; i < n; i++){
cout << a[i];
}cout << endl;
}
int main() {
char arr[] = {'h', 'e', 'l', 'l', 'o'};
char arr2[] = "hello";
printArr(arr, 5);
printArr(arr2, 5);
return 0;
}
运行结果:
hello
hello
字符串
将单个字符串在一起的一串字符叫做字符串,对于字符串"hello"来说,该字符串占6个字节,多出来的1个字节是因为字符串结尾默认存在空字符’\0’,这是字符串最重要的特点!
所以对于字符数组来说,当结尾为数值0或者空字符’\0’的时候才能称之为字符串。(字符\0代表空字符,其ASKII值为0)
在C语言中,我们通常用字符数组存储字符串,用字符数组存储字符串要注意必须有结尾字符’\0’
验证过程:打印字节大小
#include <iostream>
using namespace std;
int main() {
char a1[] = {'h', 'e', 'l', 'l', 'o'};
char a2[] = "hello";
cout << sizeof(a1) << endl << sizeof(a2) << endl;
return 0;
}
运行结果:
5
6
验证过程:给字符数组,补末尾结尾\0
#include <iostream>
using namespace std;
int main() {
char a1[] = {'h', 'e', 'l', 'l', 'o', '\0'};
char a2[] = "hello";
cout << sizeof(a1) << endl << sizeof(a2) << endl;
cout << a1 << endl;
cout << a2 << endl;
return 0;
}
运行结果:
6
6
hello
hello
字符串的输入和输出
尝试使用char数组,完成字符串的输入和输出操作
#include <iostream>
using namespace std;
char s[110];
int main() {
cin >> s;
cout << s << endl;
return 0;
}
运行结果:
hello
hello
string类和基本操作
string类
在C++中,我们通常用string来存储字符串,string是C++独有的,string的底层实现是char数组,所以string声明的对象既可以当成整个字符串进行使用,也可以当成字符数组的数组名使用
用string声明一个字符串对象,通过操作字符串对象可以实现对字符串的初始化、拷贝、比较、拼接、打印等操作。
string的基本操作
用string声明一个字符串对象,通过操作字符串对象可以实现对字符串的初始化、拷贝、比较、拼接、打印等操作
#include <iostream>
#include <string>
using namespace std;
int main() {
//通过string类声明了一个对象s
string s = "hello";
string s2 = "world";
// 字符串的拼接
s += " " + s2;
// 字符串的拷贝
s = s2;
cout << s << endl; // 作为字符串
// 作为字符数组
for(int i = 0; i < s.length(); i++) {
cout << s[i];
}
cout << endl;
string str1 = "abc";
string str2 = "abd";
// 字符串比较原则:从起始位置开始逐一比较,ASKII大小
if(str1 < str2){
cout << str1 << "<" << str2 << endl;
}
return 0;
}
string的输入与输出
string s;
// s有两种输入方式
cin >> s; //遇到空格或者换行输入结束
getline(cin, s); //遇到换行输入结束
cout << s << endl;
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
cout << s << endl;
return 0;
}
运行结果:用cin输入,遇到空格后面就没了
hello world
hello
getline输入,包含空格
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s);
cout << s << endl;
return 0;
}
运行结果:
hello world
hello world
string的相关函数
string的成员函数
size()
length()
获取字符串s中字符元素的个数
substr()
返回一个截取后的临时字符串,注意原串不改
erase()
删除字符串中的元素
find()
查询字符串中的目标字符
string s; // 定义一个字符串对象s
s.size() / s.length() // 获取字符串s中字符元素的个数
s.substr() // 返回一个截取后的临时字符串,注意原串不改
s.erase() // 删除字符串中的元素
s.find() // 查询字符串中的目标字符
练习:
#include <iostream>
#include <string>
using namespace std;
void test01(){
// size() / length()
string s = "hello";
cout << s.size() << endl; // 按照字节数计算个数
cout << s.length() << endl;
}
void test02(){
// s.substr(pos, count); 字符串截取
string s = "hello world";
cout << s.substr(6) << endl;
//cout << s.substr(6, 5) << endl;
cout << s << endl;
}
void test03(){
// s.erase; 字符串删除
string s = "hello world";
//s.erase(5); // 从s中下标5的位置删除到最后
s.erase(5, 1);// 从s中下标5的位置开始删除,只删除一个
cout << s << endl;
}
void test04(){
//s.find() 查找
string src = "aello world hello world";
string dst = "hello";
//src.find(dst) 在src中查询dst
//如果查到了,那么返回dst在src中第一次出现时字符串首元素的下标
//如果没有找到,那么返回-1, string::npos
int ret = src.find(dst);
/*
if(ret != string::npos){
cout << ret << endl;
}
else{
cout << "no" << endl;
}
*/
if(ret != -1){
cout << ret << endl;
}
else{
cout << "no" << endl;
}
}
int main() {
cout << "test01 ======" << endl;
test01();
cout << "test02 ======" << endl;
test02();
cout << "test03 ======" << endl;
test03();
cout << "test04 ======" << endl;
test04();
return 0;
}
运行结果:
test01 ======
5
5
test02 ======
world
hello world
test03 ======
helloworld
test04 ======
12
关于string的数值转换函数
stio()
将string类型转换为int
类型
stod()
将string类型转换为double类型
to_string()
将其他类型转换为string
类型
#include <iostream>
#include <string>
using namespace std;
void test01(){
string s = "1234";
// 将string转为int
int i = stoi(s);
// 用++i验证有没有变
cout << ++i << endl;
}
void test02(){
string s = "3.14";
// string转为double
double d = stod(s);
cout << 2 * d << endl;
}
void test03(){
int i = 1234;
double pi = 3.14;
cout << to_string(i) + to_string(pi) << endl;
}
int main() {
cout << "====== test01 ======" << endl;
test01();
cout << "====== test02 ======" << endl;
test02();
cout << "====== test03 ======" << endl;
test03();
return 0;
}
运行结果:
====== test01 ======
1235
====== test02 ======
6.28
====== test03 ======
12343.140000
字符串的经典应用
实战演练:字符串回文(正读反读都一样)
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string s){
// i
// j
// abc
int n = s.length();
// i指向字符串第一个,j指向字符串最后一个
int i = 0, j = n - 1;
while(i < j){
// 如果不相同,立即返回false
if(s[i] != s[j]) return false;
i++, j--;
}
return true;
}
int main() {
string s;
cin >> s;
if(isPalindrome(s)) cout << "yes" << endl;
else cout << "no" << endl;
return 0;
}
运行结果:
aba
yes
实战演练:字符串翻转
#include <iostream>
#include <string>
using namespace std;
bool reverseStr(string& s){
int n = s.length();
// i指向字符串第一个,j指向字符串最后一个
int i = 0, j = n - 1;
while(i < j){
// 前后交换
swap(s[i], s[j]);
i++, j--;
}
return true;
}
int main() {
string s;
cin >> s;
reverseStr(s);
cout << s << endl;
return 0;
}
运行结果:
nathan
nahtan
实战演练:大小写转换
#include <iostream>
#include <string>
using namespace std;
string delSpace(string s){
string t;
for(int i = 0; i < s.length(); i++){
if(s[i] != ' ') t += s[i];
}
return t;
}
void tolower(string& s){
for(int i = 0; i < s.length(); i++){
if(s[i] >= 'A' && s[i] <= 'Z')
s[i] += 32; // 转小写
}
}
int main() {
string str1, str2;
// 有空格 保留
getline(cin, str1);
getline(cin, str2);
str1 = delSpace(str1); tolower(str1);
str2 = delSpace(str2); tolower(str2);
if(str1 == str2) cout << "yes" << endl;
else cout << "no" << endl;
return 0;
}
运行结果:
A bc
a BC
yes
实战演练:字符去重
#include <iostream>
#include <string>
using namespace std;
bool vis[26];
int main() {
string s;
cin >> s;
for(int i = 0; i < s.length(); i++){
if(!vis[s[i] - 'a']){
cout << s[i];
vis[s[i] - 'a'] = 1;
}
}
return 0;
}
运行结果:
aksdfsd
aksdf
实战演练:字符排序,按字典序abc…
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool vis[26];
int main() {
string s;
cin >> s;
sort(s.begin(), s.end()); // 前闭后开原则
cout << s << endl;
return 0;
}
运行结果:
asggaewa
aaaeggsw
实战演练:字符串的查找 有无出现
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string src, dst;
getline(cin, src);
getline(cin, dst);
int ret = src.find(dst);
if(ret != -1){
cout << ret << endl;
}
else{
cout << -1 << endl;
}
return 0;
}
运行结果:
hello
a
-1
实战演练:字符串的查找 出现次数
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string src, dst;
getline(cin, src);
getline(cin, dst);
int pos = 0, ans = 0;
// src.find(dst, pos) 查dst中 从pos位置开始 src有无出现 返回下标
while((pos = src.find(dst, pos)) != -1){
ans++;
pos++;
}
if(!ans) cout << -1 << endl;
cout << ans << endl;
return 0;
}
运行结果
to be or not to be a question
to
2
实战演练:字符串的查找 出现次数 单词匹配
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
// to be or not to be a question to
// i want to go shoping today
string src, dst;
getline(cin, src);
getline(cin, dst);
// 两边补空格
src = ' ' + src + ' ';
dst = ' ' + dst + ' ';
int pos = 0, ans = 0;
// src.find(dst, pos) 查dst中 从pos位置开始 src有无出现 返回下标
while((pos = src.find(dst, pos)) != -1){
ans++;
pos++;
}
if(!ans) cout << -1 << endl;
cout << ans << endl;
return 0;
}
运行结果:
i want to go shoping today
to
1
string数组
实战演练:找到长度最长的单词
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int n;
string a[110];
int main() {
//i want to go shoping today
cin >> n;
int maxlen = 1, maxIndex = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
if(a[i].length() > maxlen){
maxlen = a[i].length();
maxIndex = i;
}
}
cout << a[maxIndex] << endl;
return 0;
}
运行结果:
6
i want to go shoping today
shoping
实战演练:按单词首字母排序
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int n;
string a[110];
int main() {
//i want to go shoping today
cin >> n;
int maxlen = 1, maxIndex = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a + 1, a + n + 1);
for(int i = 1; i <=n; i++) cout << a[i] << " ";
return 0;
}
运行结果:
5
hello
world
apple
yellow
blue
apple blue hello world yellow
小项目:密码识别
我们都申请过账号和设置密码,设置密码时通常会被一些条件限制,比如密码长度要满足8~16位,密码中至少要包含一个数字、大写字母和小写字母,我们就模拟这些条件写一个自己的密码识别功能吧!
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
// 密码识别
bool pwdRecognition(string pwd){
//1.密码长度8~16位
int len = pwd.length();
if(len < 8 || len > 16){
cout << "密码长度必须为8~16位,请重新输入:" << endl;
return false;
}
//2.密码中至少要包含一个数字、大写、小写字母、不能包含空格还有特殊字符
bool flag1 = 0, flag2 = 0, flag3 = 0;
for(int i = 0; i < len; i++){
if(pwd[i] >= '0' && pwd[i] <= '9') flag1 = 1;
else if(pwd[i] >= 'a' && pwd[i] <= 'z') flag2 = 1;
else if(pwd[i] >= 'A' && pwd[i] <= 'Z') flag3 = 1;
else if(pwd[i] == ' ') {
cout << "密码中不能包含空格,请重新输入:" << endl;
return false;
}
else{
cout << "密码中不能包含特殊字符,请重新输入:" << endl;
return false;
}
}
if(flag1 && flag2 && flag3){
cout << "密码设置成功!";
return true;
}
else{
cout << "密码中至少要包含一个数字、大写、小写字母,请重新输入:" << endl;
return false;
}
}
int main() {
string pwd;
cout << "请设置密码:" << endl;
while(1){
getline(cin, pwd);
bool ret = pwdRecognition(pwd);
if(!ret){
continue;
}
else{
break;
}
}
return 0;
}
运行结果:
请设置密码:
hello world
密码中不能包含空格,请重新输入:
123456789
密码中至少要包含一个数字、大写、小写字母,请重新输入:
123456789a
密码中至少要包含一个数字、大写、小写字母,请重新输入:
ABCabc123
密码设置成功!