Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

A timetable friendly name.

Teacher code

Try

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

Strings are zero indexed arrays of characters. Therefore you can index them like this:

string word = "Hello";
Console.WriteLine(word[1]);

This code would output the letter “e”. The number 1 can be replaced with a variable. Be careful not to reference an index longer than the number of characters in the string minus one.

The new commands used in this program and others that may be useful. Select them below to learn more:

x = y.IndexOf(z)

x is assigned the index of the first instance of string z in string y. Returns -1 if the string is not found.

x = w.Substring(y, length)

x is assigned to be a substring of length length characters from string w starting at index y. E.g. "Hello".Substring(2, 3) is “llo”. This is useful for extracting a string from the beginning or middle of another string.

x = y.Substring(y.Length - z)

x is assigned to be the substring in string y starting at index y.Length - z (i.e. the last z characters). This is useful for extracting a string from the end of another string.

x = w.Replace(y, z)

x is assigned to be the string w with all instances of string y replaced with string z.

x = string.Join(y, z)

x is assigned to be a string concatenated from all elements of array z separated by string y.

x = y.Split(z)

x is assigned to be a new array from string y separated by character z.

x = y.Trim(z)

x is assigned to be string y with all leading and trailing optional characters z removed.

x = x.Trim();

Would remove any white space or escape characters at the beginning or end of string x.

x = new string(y, z)

x is assigned to be character y repeated z times. E.g.

string x = new string('@', 5);

Would result in x = “@@@@@”.

Investigate

Questions to think about with this program to check your understanding:

Purpose question

What is the purpose of the line:

int space = teacher.IndexOf(" ");
Reveal answer

To find the index of where the space is in the teacher string. You need to know where the space is so that you can extract the next two letters.

Reason question

What is the reason for using space + 1 and number - 1 in the Substring call?

Reveal answer

space + 1 is the index of the first character after the space. number - 1 is the length we want to copy — we already took the first letter, so we need number - 1 more characters after the space.

Make

Change the program so that:

  1. The user can enter the number of letters to output.
  2. An apostrophe in the name is removed.

Typical inputs and outputs from the program would be:

Enter the name of the teacher: 
john smith
How many letters: 
3
JSM
Enter the name of the teacher: 
john smith
How many letters: 
5
JSMIT
Enter the name of the teacher: 
Dave O'Loughlin
How many letters: 
4
DOLO
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// Teacher code program

using System;

class Submission
{
---
    // -------------------------
    // Subprograms
    // -------------------------
---
    static string tcode(string teacher, int number)
    {
---
        // Remove apostrophe
        teacher = teacher.Replace("'", "");
---
        string[] letters = new string[2];
---
        // Extract first letter
        letters[0] = teacher.Substring(0, 1);
---
        int space = teacher.IndexOf(" ");
---
        // Extract number of letters after the space
        letters[1] = teacher.Substring(space + 1, number - 1);
---
        // Join letters together into a single string
        string teacher_code = string.Join("", letters);
---
        // Convert to upper case
        teacher_code = teacher_code.ToUpper();
        return teacher_code;
---
    }


---
    // -------------------------
    // Main program
    // -------------------------
    public static void Main(string[] args)
    {
---
        Console.WriteLine("Enter the name of the teacher: ");
        string teacher = Console.ReadLine();
---
        Console.WriteLine("How many letters: ");
        int number = Convert.ToInt32(Console.ReadLine());
---
        string teacher_code = tcode(teacher, number);
        Console.WriteLine(teacher_code);
---
    }
---
}