A timetable friendly name.
Select the button below to open the C# program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.
Watch this video to learn about the new concepts shown in the program:
Strings are zero indexed sequences of characters that behave like an array 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 is assigned the index of the first instance of string z in string y. Returns -1 if the string is not found.
Returns a string within x starting from index y (inclusive) to z (exclusive).
x is assigned to be the string w with all instances of string y replaced with string z.
x is assigned to be a string concatenated from all elements of array z separated by string y.
x is assigned to be a new list from string y separated every time string z occurs in y.
x is assigned to be string y with all leading and trailing optional characters z removed. If not specified, whitespace and hidden escape characters will be removed.
x = x.Trim()
Would remove any white space or escape characters at the beginning or end of string x.
x is assigned to be the string y, z times. E.g.
string x = new string("@", 5)
Would result in x = "@@@@@"
Questions to think about with this program to check your understanding:
What is the purpose of line 14:
int space = teacher.IndexOf(" ");
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.
What is the reason for using `space + 1` and `2` in line 15?
`space + 1` is the index of the first character after the space. `2` means return two characters after the space.
Change the program so that:
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
Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.
Run your code to check that your program has met the success criteria and produces the required output.
Enter the name of the teacher: Craig Sargent
How many letters: 3
CSA
Enter the name of the teacher: Craig Sargent
How many letters: 5
CSARG
Enter the name of the teacher: Dave Hillyard
How many letters: 2
DH
Enter the name of the teacher: Dave Hillyard
How many letters: 4
DHIL
Enter the name of the teacher: Sam O'Toole
How many letters: 3
SOT