String CS Thinking Example 3
Follow the full solution, then compare it with the other examples linked below.
Example 3
easyGiven name = 'Alice', what does name + ' is ' + STRING(15) + ' years old' produce?
Solution
- 1 Step 1: Convert 15 to string: '15'.
- 2 Step 2: Concatenate: 'Alice' + ' is ' + '15' + ' years old' = 'Alice is 15 years old'.
Answer
'Alice is 15 years old'
Building strings by concatenating parts is a fundamental operation for creating output messages, labels, and user-facing text.
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 โMore String Examples
Example 1 easy
Given s = 'Hello World', what is: (a) LENGTH(s), (b) s[0], (c) s[6..10] (substring from index 6 to 1
Example 2 mediumWhat is the result of concatenating 'Score: ' + STRING(95) + '%'?
Example 4 mediumGiven word = 'PROGRAMMING', what is the result of each operation? (a) LENGTH(word) (b) word[0] + wor