ENGR/CS 101 CS Session Lecture 5 Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2010 Has everyone finished the exercise from last time so that their program will encipher one uppercase letter? Lecture 5 ENGR/CS 101 Computer Science Session 1 Outline Problem: input more than one letter at a time C# programming language Strings and indexing For-loops Lecture 5 ENGR/CS 101 Computer Science Session 2 Problem Specification Today we will modify our GUI program to accept and encipher whole words in uppercase letters, rather than just a single letter. Lecture 5 ENGR/CS 101 Computer Science Session 3 Strings Enciphering one letter at a time is tedious. Want to handle an entire word (or sentence). Design: Encipher each letter in the plaintext box. Need to use a string, rather than a character. A string is a sequence of characters. The Text property of a textbox is a string. We can access it using plaintextBox.Text; Lecture 5 ENGR/CS 101 Computer Science Session 4 Indexing Individual characters are accessed by giving the index of the character in the string. 'A' 'C' 'E' 'S' [0] [1] [2] [3] The syntax for accessing an individual character is: <string var>[<index>] Example: plaintextBox.Text[2] is the character 'E' Lecture 5 ENGR/CS 101 Computer Science Session 5 Indexing As shown on the previous slide, the indexes of a string start at 0. We can use indexing to get the key letter, too. It is the first character in the keyBox.Text string. shiftKey = keyBox.Text[0]; Lecture 5 ENGR/CS 101 Computer Science Session 6 For-Loops An index used to access string characters may be any integer expression. In particular, it may be a variable. So we can make a variable count from 0 to the last index of the string to access each character one at a time. We do this with a for-loop (also called a counting loop). A for-loop consists of 4 parts: Lecture 5 A loop control variable and its initial value A loop condition test of the loop control variable A loop control variable update A body of steps that are repeated ENGR/CS 101 Computer Science Session 7 For-Loops For our Encipher handler design, we would use the for-loop in the following way (changes in bold). 1. Clear ciphertext box 2. For index i from 0 to length of plaintextBox.Text by 1 2.1. Get the shift key from keyBox.Text[0] 2.2. Get the plaintext letter from plaintextBox.Text[i] 2.3. Compute the corresponding ciphertext letter 2.4. Append the ciphertext letter to the ciphertext box. The loop control variable is i, with initial value of 0. It is updated by adding 1 to i until it reaches the length of the plaintextBox.Text string. Lecture 5 ENGR/CS 101 Computer Science Session 8 For-Loops The syntax of a for-loop in C# is: for ( <lcv declaration/initialization>; <loop condition>; <lcv update statement>) { <steps to be repeated> } For our program, we would write: for (int i = 0; i < plaintextBox.Text.Length; i++) { // steps to be repeated } Lecture 5 ENGR/CS 101 Computer Science Session 9 For-Loops Some notes: Lecture 5 Clearing the ciphertext box should only be done once, so it happens before the loop starts. The length of a string is obtained using the Length property, accessed with the dot notation (e.g., plaintextBox.Text.Length). It is one more than the last index, so the loop condition uses < rather than <=. More on conditions next class. The loop body is computing the ciphertext letter and appending it to the ciphertext box in the same way as the last program. ENGR/CS 101 Computer Science Session 10 In-class Exercise Modify today's program to encipher a line of plaintext and display the ciphertext using a forloop (code shown on the last two slides): Lecture 5 Add a for-loop around the statements that correspond to steps 2.1 - 2.4 on Slide 8. Note that the body of the for-loop is exactly the same code as before, except for computing shiftKey and plainLetter using indexing instead of char.Parse( ) ENGR/CS 101 Computer Science Session 11 In-class Exercise Test your program with the string "ACES" => "IKMA" See what happens if you type in "Go Aces!" We'll fix this "problem" next class. Lecture 5 ENGR/CS 101 Computer Science Session 12 Putting the Code Together // The modified parts from the previous program are bold // Everything is the same on this page. // Variable declarations char shiftKey, // key letter plainLetter, // user input cipherLetter;// result int shiftNumber, // # of shift places index; // of cipher letter // Clear the result box ciphertextBox.Text = ""; Lecture 5 ENGR/CS 101 Computer Science Session 13 Putting the Code Together // For each letter in plaintext box, indexed by i for (int i = 0; i < plaintextBox.Text.Length; i++) { // Use indexing to get shift key and plaintext letter shiftKey = keyBox.Text[0]; plainLetter = plaintextBox.Text[i]; // The rest is exactly the same as before // Compute the corresponding ciphertext letter shiftNumber = shiftKey ­ 'A'; index = (plainLetter ­ 'A' + shiftNumber) % 26; cipherLetter = (char)((int)'A' + index); // Append the enciphered letter to ciphertext box ciphertextBox.AppendText(cipherLetter.ToString()); } // don't forget the closing curly brace Lecture 5 ENGR/CS 101 Computer Science Session 14