Files
ClassFeedback/output/CSP03-第8课-课前默写-string(一)复习.md

158 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# CSP03班 第8课 课前默写10分钟
**班级**CSP03班周六早/下午、周日早上)
**范围**第7课《string字符串使用
**满分**100分
**建议用时**10分钟
---
## 一、填空题(每空 4 分,共 32 分)
1. 声明一个 `string` 变量 `s` 并初始化为 `"hello"` 的语句是:
`string s = _______________ ;`
2. 获取字符串 `s` 长度的函数是 `s._______()`
3. 读取一整行字符串(**包含空格**)应该使用函数:
`_______________(cin, s);`
4. `cin >> s` 读取字符串时,遇到 **_______** 就会停止读取。
5. 在字符串 `s` 中查找子串 `"abc"`,应该调用:
`s._______________("abc")`
6. 如果 `find()` 函数没有找到目标子串,返回值是 **_______**
7. 字符串 `"abcdef"` 中,字符 `'c'` 的下标是 **_______**
8. 从字符串 `s` 的下标 `2` 开始截取 `3` 个字符,应写成:
`s._______________(2, 3)`
---
## 二、判断题(每题 4 分,共 16 分)
正确的打 `√`,错误的打 `×`
1. `string s = "abc"; s += "def";` 执行后,`s` 的值是 `"abcdef"`
2. `string::npos` 的值等于 `-1`
3. )两个 `string` 可以直接用 `==` 比较是否相等。
4. `getline(cin, s)` 可以读取包含空格的整行字符串,直到遇到回车。
---
## 三、代码填空(每空 4 分,共 24 分)
下面程序的功能是:输入一行字符串,统计其中**大写字母**的数量。请补全代码。
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
_______________(cin, s); // ① 读入一整行
int cnt = 0;
for (int i = 0; i < s.___________(); i++) { // ② 遍历字符串
if (_______(s[i])) { // ③ 判断是否为大写字母
cnt++;
}
}
cout << cnt << endl;
return 0;
}
```
---
## 四、代码阅读(共 10 分)
阅读以下程序,写出输出结果:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello world";
cout << s.find("world") << endl; // 输出①________
if (s.find("xyz") == string::npos) {
cout << "YES" << endl; // 输出②________
} else {
cout << "NO" << endl;
}
return 0;
}
```
---
## 五、简答题(共 30 分)
1. **简述 `cin >> s` 和 `getline(cin, s)` 在读取字符串时的主要区别。**10分
答:
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
2. **写出一段代码,遍历字符串 `s`,将其中的所有大写字母转换为小写字母。**20分
答:
```cpp
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
```
---
## 参考答案(教师用,不要发给学生)
### 一、填空题
1. `"hello"`
2. `size` 或 `length`
3. `getline`
4. 空格
5. `find`
6. `string::npos`
7. `2`
8. `substr`
### 二、判断题
1. ``
2. `×``string::npos` 是一个极大值,不是 -1
3. ``
4. ``
### 三、代码填空
① `getline`
② `size` 或 `length`
③ `isupper`(或 `s[i] >= 'A' && s[i] <= 'Z'`
### 四、代码阅读
输出①:`6`
输出②:`YES`
### 五、简答题
1. `cin >> s` 遇到空格会停止,只能读取一个单词;`getline(cin, s)` 会读取整行,包括空格,直到遇到换行符。
2. ```cpp
for (int i = 0; i < s.size(); i++) {
s[i] = tolower(s[i]);
}
```
或范围 for 循环写法亦可。
---
*穹狼科创 · CSP03班 · 2026春季*