Answer to Exercise 1

This exercise shouldn't have caused you too much difficulty. If it did, then you need to revise the section on String Manipulation. We can't emphasize enough how much easier your life gets as a programmer once you have a good understanding of how to manipulate strings with the inbuilt methods, functions or subs.

The key to this exercise is to use Substring to chop the notes variable into two parts. You get the first part by specifying 0 and 2 between the round brackets of Substring. You get the second part by specifying a 2 between the round brackets of Substring. This will grab the characters from position 2 to the end of the string. You then join the two parts together.

 

C# NET Answer

private void btnKeyC_Click(object sender, EventArgs e){

	string notes = "ABCDEFG";

	string keycend = notes.Substring(0, 2);

	string keycstart = notes.Substring(2);

	string keyc = keycstart + keycend;

	listBox1.Items.Add(keyc);
}

 

VB NET Answer

Private Sub btnKeyC_Click(sender As Object, e As EventArgs) Handles btnKeyC.Click

	Dim notes As String = "ABCDEFG"
	Dim key_c_end As String
	Dim key_c_start As String
	Dim key_of_c As String

	key_c_end = notes.Substring(0, 2)
	key_c_start = notes.Substring(2)

	key_of_c = key_c_start & key_c_end

	ListBox1.Items.Add(key_of_c)
	
End Sub

 

Java Answer

	public static void main(String[] args) {
        
        String notes = "ABCDEFG";
        
        String keycend = notes.substring(0, 2);
        String keycstart = notes.substring(2);
        
        String keyc = keycstart + keycend;
        
        System.out.println(keyc);
 
    }