Pascal-Chapter Strings

Chapter 5 : Strings

Red alert captain! When you powered the ship, the engines exploded… Seems like someone sabotaged the igniter! We should find out later. For now, you need to replace the damaged part.

Strings are defined using the built-in string type as follows:

const a: string = “Hello Captain Rogers”;

Concatenating Strings

Strings can be concatenated using the ^ operator.

const name: string = “Captain Rogers”
const greeting: string = “Hello”
const full_greeting: string = greeting ^ ” ” ^ name

Slicing Strings

Strings can be sliced using a built-in function String.sub which takes three parameters:

– an offset describing the index of first character that will be copied

– the length describing the number of characters that will be copied (starting from the given offset)

– the string being sliced

The function String.sub can be used as follows:

const name: string = “Captain Rogers”
const slice: string = String.sub(0n, 1n, name)

⚠️ Notice that the offset and length of the sub function are natural numbers.

Length of Strings

The length of a string can be found using a built-in function String.length as follows:

const name: string = “Captain Rogers”
const length: nat = String.length(name) // length = 14

Your mission

1 – Reassign my_ship by modifying the engine attribute (third number) from 0 to 1. Use substrings for the attributes before and after to make sure they are untouched.

AWAITING VALIDATION

Type your solution above and validate your answer