String CS Thinking Example 1
Follow the full solution, then compare it with the other examples linked below.
Example 1
easyGiven s = 'Hello World', what is: (a) LENGTH(s), (b) s[0], (c) s[6..10] (substring from index 6 to 10)?
Solution
- 1 Step 1: (a) Count all characters including the space: LENGTH = 11.
- 2 Step 2: (b) s[0] = 'H' (first character, zero-indexed).
- 3 Step 3: (c) s[6..10] = 'World' (characters at indices 6, 7, 8, 9, 10).
Answer
(a) 11, (b) 'H', (c) 'World'.
Strings are sequences of characters accessed by index. Key operations include finding length, accessing individual characters, and extracting substrings.
About String
A data type that represents a sequence of characters (text), enclosed in quotation marks. Strings support operations like concatenation (joining), slicing (extracting substrings), searching, and comparison, and can contain letters, digits, spaces, and special characters.
Learn more about String โ