Lesson 75: String handling
Exam strings and C++ strings
| Exam | C++ |
|---|---|
| LENGTH(T) | t.length() |
| UCASE(T) | t.toUpperCase() |
| LCASE(T) | t.toLowerCase() |
| SUBSTRING (T, 7, 16) |
t.substring(6, 22) |
-
toUpperCase()changes the string itself; it does not return a new one. -
The exam counts positions from 1 and gives a length. C++ counts from 0 and gives the index just after the end.
-
So exam position 7 is C++ index 6, and 16 characters later the end is index 22.
In the exam
- 0478/23/M/J/24 Q7(a): store "IGCSE Computer Science at Cambridge", extract and output "Computer Science", and output the string in upper case. Mark scheme example: PartText ← SUBSTRING(FullText, 7, 16); OUTPUT PartText, UCASE(FullText).
- 0478/23/O/N/24 Q10: trace a password checker. (b) Its rules, from the mark scheme: 8 to 20 characters; not all upper case or all lower case; it must contain a !. Your second program checks the same rules.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
In exam pseudocode, SUBSTRING(Text, Start, Length) starts at position 1. C++ s.substring(from, to) starts at 0 and stops just trước khi to. Write the function SUBSTRING so it works like the exam one: then SUBSTRING(fullText, 7, 16) gives Computer Science.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 4/5 · 280 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
The exam password rules: 8 to 20 characters; not all lower case and not all upper case; it must contain a !. Finish accept so it returns false when a rule is broken. Type passwords in the Serial Monitor.
Nhấn Chạy để xem kết quả ở đây.