School timetables often show the teacher of a lesson using a three character teacher code.
00:08
There are lots of different standards for this, but one is the first letter of the teacher's forename, followed by the first two letters of their surname.
00:16
For example, Dave Hilliard would be dhi.
00:20
This programme outputs a teacher code using this format from a full name.
00:26
As always, we start with the boilerplate code.
00:33
Let's write the difficult function first.
00:36
Static string T code string teacher.
00:42
So this function identified as tcode, will return a string.
00:46
That's the three digit code using the string parameter teacher, which will be their name.
00:54
Lets store the two parts of the teacher code.
00:57
That's the first initial and then the two letters from the surname in an array identified as letters.
01:05
So string letters becomes a new string array with two elements.
01:10
This gives us somewhere to store the two parts.
01:13
We could have used two variables here.
01:14
but it's just an opportunity to practise using an array again.
01:20
The first part of the teacher code is easy.
01:22
Strings are actually arrays of characters to a computer, so we can use indexes on strings too.
01:31
It's helpful to see a visualisation of this.
01:34
Here we can see the teacher's name is a string made up of 13 indexes numbered 0 to 12.
01:41
With each element storing a single character.
01:44
We can see that the first initial D is at index zero.
01:49
Being able to use strings as arrays is really useful.
01:53
Ok, back to the programme.
01:55
The first part of the teacher code, that's letters index zero becomes the teacher, but a substring of it at index zero for one character, that's the letter D.
02:08
The second part is trickier.
02:10
Let's look at another visualisation.
02:13
The surname starts after a space, so if we can find the index of the space, then we can use that number to extract the other characters, in this case 5 and 6.
02:25
It's important that we find where the space is because different four names are of different length, so the space won't always be in the same place for different names.
02:35
Back to the programme.
02:37
So the position of the space, an integer be becomes teacher, dot index of and then the data to find in brackets, that's a space open quote, space, close quote.
02:53
It's handy that strings have methods that we can use in this way to manipulate them.
02:58
Letters one, that's the second part of the code becomes teacher at the index of space plus one because we don't want to include the space.
03:08
And then two more characters.
03:12
Great.
03:12
Now we need the teacher code to be a variable and not an array.
03:16
You can join elements of an array together using join a string variable.
03:22
Teacher code becomes a string joined together with nothing separating them all the elements in the letters array.
03:32
Finally, we want the code in uppercase, so teacher code becomes itself toupper.
03:39
return teacher code.
03:43
For such a simple output, there's a lot going on here and I've tried to show you lots of concepts in one small programme.
03:49
Strings are, arrays of characters, so they have indexes.
03:54
You can extract single characters using an index.
03:58
You can find the index of one string inside another and you can extract a substring.
04:04
You can join array, elements together to become a single string.
04:08
And you can change a variable into upper or lowercase too.
04:13
To finish, all we need to do is ask the user to input the name of the teacher, set the teacher code to become the result of our TCODE function, passing in the name of the teacher to that function and outputting the teacher's code.
04:30
Running the programme shows the expected output from my name.