🏰 第1关:寻找第一个孤独字符
1、🎯题目
输入一个字符串,输出第一个只出现一次的字符。
如果没有,输出No。
(1)输入
abaccdeff(2)输出
b2、🧚故事
字符村里住着很多字母居民:
a 出现2次
b 出现1次
c 出现2次
只有b没有双胞胎,所以它最孤独!
3、🧠思路
先统计每个字符出现次数,再从左到右找第一个次数=1的字符。
4、✅代码
#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; int cnt[256] = {0}; for(char c : s) cnt[c]++; for(char c : s) { if(cnt[c] == 1) { cout << c; return 0; } } cout << "No"; return 0; }🏰 第2关:反转字符串魔法
1、🎯题目
输入字符串,倒着输出。
(1) 输入
hello(2) 输出
olleh2、🧚故事
魔法师把单词镜像翻转啦!
hello → olleh
3、🧠思路
从最后一个字符往前输出。
4、✅代码
#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; for(int i = s.size()-1; i >= 0; i--) cout << s[i]; return 0; }🏰 第3关:统计元音精灵
1、🎯题目
统计字符串中 a e i o u 的个数。
(1)输入
education(2)输出
52、🧚故事
森林里住着5种元音精灵:
a e i o u
数一数来了几个!
3、🧠思路
遍历字符串,遇到元音就加1。
4、✅代码
#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; int ans = 0; for(char c : s) { if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u') ans++; } cout << ans; return 0; }🏰 第4关:回文城堡
1、🎯题目
判断字符串是否回文(正读反读一样)。
(1)输入
level(2)输出
Yes2、🧚故事
回文城堡的大门只有对称密码才能打开!
level ✔
abc ❌
3、🧠思路
左右指针比较。
4、✅代码
#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; int l = 0, r = s.size()-1; while(l < r) { if(s[l] != s[r]) { cout << "No"; return 0; } l++; r--; } cout << "Yes"; return 0; }🏰 第5关:大写变身术
1、🎯题目
把小写字母变成大写字母。
输入
hello输出
HELLO2、🧚故事
小写士兵经过魔法门后变成大写勇士!
3、🧠思路
小写字母 -32 就变大写。
4、✅代码
#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; for(char &c : s) { if(c >= 'a' && c <= 'z') c = c - 32; } cout << s; return 0; }🏰 第6关:单词计数器
1、🎯题目
输入一行句子,统计单词数量(单词之间空格分开)。
(1)输入
I love Cpp(2)输出
32、🧚故事
句子火车来了,每节车厢是一个单词!
3、🧠思路
统计从空格后开始的新单词。
4、✅代码
#include <iostream> #include <string> using namespace std; int main() { string s; getline(cin, s); int cnt = 0; bool inWord = false; for(char c : s) { if(c != ' ' && !inWord) { cnt++; inWord = true; } if(c == ' ') inWord = false; } cout << cnt; return 0; }🏰 第7关:最长连续相同字符
1、🎯题目
输出最长连续相同字符长度。
(1)输入
aaabbccccdd(2)输出
42、🧚故事
字符排队站岗,看看谁的队伍最长!
3、🧠思路
记录当前连续长度,更新最大值。
4、✅代码
#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; int mx = 1, cur = 1; for(int i = 1; i < s.size(); i++) { if(s[i] == s[i-1]) cur++; else cur = 1; if(cur > mx) mx = cur; } cout << mx; return 0; }🏰 第8关:删除某字符
1、🎯题目
删除字符串中所有字符a
(1)输入
banana(2)输出
bnn2、🧚故事
国王下令:所有 a 字母离开王国!
3、🧠思路
新建答案字符串,遇到不是a才加入。
4、✅代码
#include <iostream> #include <string> using namespace std; int main() { string s, ans = ""; cin >> s; for(char c : s) { if(c != 'a') ans += c; } cout << ans; return 0; }🏰 第9关:字符排序比赛
1、🎯题目
把字符串字符按从小到大排序。
(1)输入
dcba(2)输出
abcd2、🧚故事
字符运动会开始啦!按字典顺序排队!
3、🧠思路
使用 sort 排序。
4、✅代码
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string s; cin >> s; sort(s.begin(), s.end()); cout << s; return 0; }🏰 第10关:密码强度检测
1、🎯题目
判断字符串是否同时含有:
大写字母
小写字母
数字
有则输出Strong,否则输出Weak
(1)输入
Abc123(2)输出
Strong2、🧚故事
勇士密码要通过三重考验:
🛡 大写门
⚔ 小写门
💎 数字门
3、🧠思路
设置3个标记变量。
4、✅代码
#include <iostream> #include <string> using namespace std; int main() { string s; cin >> s; bool A = false, a = false, d = false; for(char c : s) { if(c >= 'A' && c <= 'Z') A = true; else if(c >= 'a' && c <= 'z') a = true; else if(c >= '0' && c <= '9') d = true; } if(A && a && d) cout << "Strong"; else cout << "Weak"; return 0; }🎁 总结:知识点
1、基础操作
✔ 遍历
✔ 修改
✔ 拼接
✔ 删除
2、常见算法
✔ 计数
✔ 查找
✔ 回文判断
✔ 连续统计
✔ 排序
3、技巧
✔ 双指针
✔ 标记法
✔ 数组统计