字符数组与字符串
概念讲解
字符数组
123456char str[100];
// 初始化
char s1[] = "hello";
char s2[10] = "world";
char s3[10] = {'h', 'e', 'l', 'l', 'o', '\0'};string 类
12345678910111213141516171819202122232425#include <string>
using namespace std;
string s = "hello";
// 访问字符
cout << s[0] << endl; // 'h'
cout << s.at(0) << endl;
// 长度
cout << s.length() << endl; // 5
cout << s.size() << endl; // 5
// 拼接
string s2 = " world";
string s3 = s + s2; // "hello world"
// 子串
string sub = s.substr(1, 3); // "ell"
// 查找
int pos = s.find("ll"); // 2
if (pos != string::npos) {
cout << "找到" << endl;
}输入输出
cin
12string s;
cin >> s; // 遇到空格停止getline
12string s;
getline(cin, s); // 读取整行字符数组
12345char s[100];
cin >> s; // 遇到空格停止
cin.getline(s, 100); // 读取整行
scanf("%s", s); // 遇到空格停止
fgets(s, 100, stdin); // 读取整行常用函数
字符数组函数
123456789101112131415161718#include <cstring>
char s[] = "hello";
// 长度
int len = strlen(s); // 5
// 比较
int cmp = strcmp(s1, s2); // 0 相等,<0 s1<s2,>0 s1>s2
// 复制
strcpy(dest, src);
// 拼接
strcat(dest, src);
// 查找
char* pos = strstr(s, "ll");string 函数
123456789101112131415161718192021222324252627string s = "hello";
// 插入
s.insert(1, "XY"); // 在位置 1 插入 "XY" → "hXYello"
// 删除
s.erase(1, 2); // 从位置 1 开始删除 2 个字符 → "hello"
// 转为 C 风格字符串(const char*)
const char* p = s.c_str(); // 返回以 '\0' 结尾的字符数组指针
// 用 char* 构造 string
const char* cstr = "world";
string s2(cstr); // s2 = "world"
// 反向迭代器构造反转串
string rev(s.rbegin(), s.rend()); // rev = "olleh"
// 大小写转换
transform(s.begin(), s.end(), s.begin(), ::toupper);
transform(s.begin(), s.end(), s.begin(), ::tolower);
// 反转
reverse(s.begin(), s.end());
// 排序
sort(s.begin(), s.end());字符处理
判断字符类型
12345678char c = 'A';
isalpha(c); // 是否是字母
isdigit(c); // 是否是数字
isalnum(c); // 是否是字母或数字
isupper(c); // 是否是大写
islower(c); // 是否是小写
isspace(c); // 是否是空白字符字符转换
123456789char c = 'a';
char upper = toupper(c); // 'A'
char lower = tolower(c); // 'a'
// 数字字符转数字
int digit = '9' - '0'; // 9
// 数字转字符
char ch = 5 + '0'; // '5'常见操作
1. 统计字符出现次数
123456int cnt[26] = {0};
for (char c : s) {
if (isalpha(c)) {
cnt[tolower(c) - 'a']++;
}
}2. 回文判断
123456789bool isPalindrome(string s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s[left] != s[right]) return false;
left++;
right--;
}
return true;
}3. 子串查找
123456789int findSubstring(string s, string pattern) {
int n = s.length(), m = pattern.length();
for (int i = 0; i <= n - m; i++) {
if (s.substr(i, m) == pattern) {
return i;
}
}
return -1;
}考点分析
初赛考查方式
- 选择题: 字符串函数的使用
- 程序阅读: 理解字符串处理过程
- 编程题: 字符串处理
常见题型
- 字符串统计
- 回文判断
- 字符串匹配
例题精讲
例题 1:统计单词数
题目: 统计字符串中单词的个数。
解答:
123456789101112131415161718192021222324#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s);
int count = 0;
bool inWord = false;
for (char c : s) {
if (c == ' ') {
inWord = false;
} else if (!inWord) {
inWord = true;
count++;
}
}
cout << count << endl;
return 0;
}例题 2:字符串反转
题目: 反转字符串。
解答:
123456789101112131415#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s;
cin >> s;
reverse(s.begin(), s.end());
cout << s << endl;
return 0;
}练习题
- 统计字符串中大写、小写字母的个数。
查看答案
12345678910111213141516171819#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s);
int upper = 0, lower = 0;
for (char c : s) {
if (isupper(c)) upper++;
else if (islower(c)) lower++;
}
cout << "大写: " << upper << endl;
cout << "小写: " << lower << endl;
return 0;
}- 判断字符串是否是回文。
查看答案
1234567891011121314151617181920212223242526#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s[left] != s[right]) return false;
left++;
right--;
}
return true;
}
int main() {
string s;
cin >> s;
if (isPalindrome(s)) {
cout << "是回文" << endl;
} else {
cout << "不是回文" << endl;
}
return 0;
}- 将字符串中的数字提取出来。
查看答案
1234567891011121314151617181920212223#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
string num = "";
for (char c : s) {
if (isdigit(c)) {
num += c;
}
}
if (num.empty()) {
cout << "没有数字" << endl;
} else {
cout << num << endl;
}
return 0;
}易错点
⚠️ 注意:
- 字符数组要预留 '\0' 的位置;若数组大小不足以容纳 '\0'(如
char s[5] = "Hello"),在 C++ 中属于编译错误- strcmp 返回值不是 bool
- getline 前要清除缓冲区
- string 下标从 0 开始
- char 数组名本质是指针,
s1 == s2比较的是地址而非内容;比较字符串内容必须用strcmp(字符数组)或==(string 类)