String CS Thinking Example 4

Follow the full solution, then compare it with the other examples linked below.

Example 4

medium
Given word = 'PROGRAMMING', what is the result of each operation? (a) LENGTH(word) (b) word[0] + word[LENGTH(word) - 1] (c) SUBSTRING(word, 3, 4)

Solution

  1. 1
    Step 1: LENGTH('PROGRAMMING') = 11 characters.
  2. 2
    Step 2: word[0] is 'P'. word[LENGTH(word) - 1] = word[10] is 'G'. Concatenation gives 'PG'.
  3. 3
    Step 3: SUBSTRING(word, 3, 4) extracts 4 characters starting at index 3 โ†’ 'GRAM'.

Answer

(a) 11, (b) 'PG', (c) 'GRAM'. String indexing starts at 0, and SUBSTRING takes a start index and a length.
Understanding string indexing (zero-based), LENGTH, and SUBSTRING are essential for text processing tasks like parsing input, extracting data, and validating formats.

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