In this post we will focus on string manipulation techniques using five common techniques used in most procedural programming languages:
- LEFT: to extract characters at the beginning of a string,
- RIGHT: to extract characters at the end of a string,
- LENGTH: to find out the number of characters in a string,
- MID: to extract a set number of characters at a given position in a string,
- LOCATE: to find if a substring is included in another string and if so return the position of this substring in the string.
>>> to extract the first 5 characters of a string.
>>> to extract the last 5 characters of a string.
>>> to retrieve the number of characters in a string.
>>> to extract a set number of characters at a given position in a string.
>>> to find if a substring is included in another string and if so return the position of this substring in the string.
If the substring is not part of the string, the value -1 is returned.
A timetabling system used by school stores information about each lesson such as the day and period of the lesson, the subject, the class teacher, and the room number. The system enables us to extract all these details in the following format:
Year Group-Subject-Day of the Week-Week Number (1 or 2):Period of the day (1 to 5)-Title (Mr/Mrs)-Teacher Name-Classroom
For examples we have extracted these 4 lessons from the system:
Check the code below which is based on the 5 string manipulation functions we mentioned above: Left, Right, Length, Mid, and Locate.
Your challenge is to tweak this code to extract all the information from a lesson code and display it in the following format:
- Lesson Code: 8-ICT-Mo1:3-Mrs.Johnson-A12
Year Group: 8
Week Day: Monday
Week Number: 1
Teacher: Mrs Johnson