Skip to content
traverse parallel array, substring, string length
This task assumes that you have already been introduced to:
using parallel arrays
using a loop to traverse an array and display element values
using substring function to select a section of a substring
using the length function to use the number of characters as part of a calculation for substring.
There is some
code to guide you through the steps in the task.
Try to work with a partner and your code
typing and running the code.
Make sure your output is
before moving from one stage to the next, ask your teacher if you need some assistance.
notice the data value
and the use of the substring and length functions.
work with a partner and agree on what you think this code will output
run the code and check the output against what you predicted (
section only for now)
amend the parameter values for the first substring to display
amend the parameter values for the second substring to display
code after investigating
you will have seen that the loop already displays the names but not yet the phone number or postcode
insert 2 lines of code within the loop (look for the
comment lines as a guide)
first display the entire postcode,
second display the entire phone number
output from loop after
Edmonds BS4_1SY 07507-304671
Sandoval HD9_7NP 07574-522632
Bridges M45_8GH 07746-842316
Blaese BS16_4QX 07818-193588
amend the loop code to display the phone number as
notice that some postcodes
characters and others
and that they all
amend the loop code to display the first part of the postcode
Make sure all postcodes and phone numbers display correctly
sample output from
Edmonds BS4 +447507304671
Sandoval HD9 +447574522632
Bridges M45 +447746842316
Blaese BS16 +447818193588
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (