Presentation is loading. Please wait.

Presentation is loading. Please wait.

07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

Similar presentations


Presentation on theme: "07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)"— Presentation transcript:

1 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

2 207/10/2015 Learning Objectives Explain how relational operators ( ) compare strings. Explain what the Instr, Mid and Len functions do.

3 307/10/2015 Character Representation Over the years different computer designers have used different sets of binary codes for representing characters in a character set. This has led to great difficulty in transferring information from one computer to another. i.e. which binary code represents each character i.e. which binary code represents each character

4 407/10/2015 ASCII (American Standard Code for Information Interchange) Represents each character in a standard character set as a single byte binary code. The standard code form that most PCs use to allow for communication between systems. Usually uses a 7 bit binary code so can store 128 different characters and simple communications protocols. Sufficient for all characters on a standard keyboard plus control codes. Can be extended (extended ASCII) to use 8 bits (so can store 256 characters) to encode Latin language characters. Can be extended (extended ASCII) to use 8 bits (so can store 256 characters) to encode Latin language characters.

5 507/10/2015 ASCII code The first 32 ASCII codes are used for simple communications protocols, not characters. e.g. ACK – acknowledge and would be sent by a device to acknowledge receipt of data. e.g. ACK – acknowledge and would be sent by a device to acknowledge receipt of data. 0110010 – 2 0110010 – 2 0110001 – 1 0110001 – 1 ….. ….. 1000001 – A 1000001 – A 1000010 – B 1000010 – B

6 607/10/2015 Representing Characters and Numbers e.g. If the ‘A’ key is pressed ‘1000001’ is sent to the CPU. If the 1 key is pressed then ‘0110001’ is sent to the CPU. If the 1 key is pressed then ‘0110001’ is sent to the CPU. If the user wants to print ‘123’ the codes for 1, 2 & 3 are sent to the printer. If the user wants to print ‘123’ the codes for 1, 2 & 3 are sent to the printer.

7 707/10/2015 Processing Strings

8 807/10/2015 Using relational operators > or or < relates to alphabetical order. e.g. The following code will show 2 strings in a list box in alphabetical order. e.g. The following code will show 2 strings in a list box in alphabetical order. Dim Name1 As String Dim Name2 As String Name1 = InputBox(“Enter a name.”) Name2 = InputBox(“Enter another name.”) If Name1 < Name2 Then lstAlphabetical.Items.Add(Name1) lstAlphabetical.Items.Add(Name1) lstAlphabetical.Items.Add(Name2) lstAlphabetical.Items.Add(Name2)Else lstAlphabetical.Items.Add(Name1) lstAlphabetical.Items.Add(Name1) End If

9 How ASCII codes are used to arrange letters in alphabetical order Letter a-z have increasing ASCII codes. Each character’s ASCII value is compared. Try using the ASCII (converts a letter into its corresponding ASCII binary code) and Char (converts a ASCII binary code into its corresponding letter) commands. 907/10/2015

10 4.1a ASCII Specification: Allow the user to convert a letter to its corresponding ASCII code and vice versa. Allow the user to convert a letter to its corresponding ASCII code and vice versa. 1007/10/2015

11 Program 4.1a ASCII Dim Letter As String Dim ASCII As Integer Console.WriteLine(“Enter a letter.”) Letter = Console.ReadLine ASCII = Asc(Letter) ‘Convert letter to ASCII Console.WriteLine(“The ASCII code for ” & Letter & “is: “ ASCII) 1107/10/2015 Continued on the next slide.

12 Program 4.1a ASCII Console.WriteLine(“Enter a denary number.”) ASCII = Console.ReadLine Letter = ChrW(ASCII) ‘Convert ASCII code to a letter. Console.WriteLine(“The ASCII code: ” & ASCII & “is: “ Letter) 1207/10/2015

13 Program 4.1a ASCII Run the program and test it. 1307/10/2015

14 The Instr function returns the position in the main string of the substring being searched for. Assume declaration of variables below. Assume declaration of variables below. MainString = “The man looked up and saw the man in the moon.” MainString = “The man looked up and saw the man in the moon.” SearchString = “ man ” SearchString = “ man ” Position = Instr(MainString, SearchString) Position = Instr(MainString, SearchString) Note that it will look only once so Instr in the example above would NOT find the second occurrence of the word “man”. 14 07/10/2015 Searching for a substring using Locate (Instr) 5

15 The Mid function returns a substring from the main string. Dim Character As String Dim Characters As String MainString = “ Keep on looking ahead. ” Character = Mid(MainString, 9, 1 ) 1507/10/2015 Processing individual characters using Mid Position How many characters. l

16 The Mid function returns a substring from the main string. Dim Character As String Dim Characters As String MainString = “ Keep on looking ahead. ” Character = Mid(MainString, 9, 2 ) 1607/10/2015 Processing individual characters using Mid Position How many characters. lo

17 Len returns the number of characters in a string. Dim EmployeeName As String Dim EmployeeName As String Dim LengthOfString As Integer Dim LengthOfString As Integer EmployeeName = “Mr Lee” EmployeeName = “Mr Lee” LengthOfString = Len(EmployeeName) LengthOfString = Len(EmployeeName) MsgBox(“The length of the Employee’s name is: “ & LengthOfString) MsgBox(“The length of the Employee’s name is: “ & LengthOfString) 1707/10/2015 Length (Len) 6

18 String Handling Functions All string handling functions give a “result”. This could be a number, character or characters. The result of a string handling function is almost always stored in a separate variable: e.g. Position = Instr(MainString, SearchString) Character = Mid(MainString, 9, 1) LengthOfString = Len(EmployeeName) You can look at this variable with If statements: e.g. e.g. If Position = 2 Then If Character = “b” Then If LengthOfString >=5 Then You can also mix these functions: e.g. e.g. LengthOfString = Len(MainString) Character = Mid(MainString, LengthOfString, 1) If Character = “e” Then Basically asking if there is an “e” at the end of the variable MainString. Basically asking if there is an “e” at the end of the variable MainString.

19 1907/10/2015 Program 4.1b Validating a Full Name Specification: Ask the user to enter a person’s surname and then their first name. Ask the user to enter a person’s surname and then their first name. Check that: Check that: That a space has not been entered at the beginning of the name. Only one space character has been used between the names.

20 2007/10/2015 Program 4.1b Validating a Full Name Create a new project named ‘Validating a Full Name’.

21 2107/10/2015 Program 4.1b Validating a Full Name Dim Spaces As Integer Dim FullName As String Dim Index As Integer Dim Character As String Console.WriteLine(“Enter a full name.”) FullName = Console.ReadLine

22 2207/10/2015 ‘Is there a space at the beginning (1 st position)? If InStr(FullName, " ") = 1 Then Console.WriteLine(“You are not supposed to have a space at the beginning.") Console.WriteLine(“You are not supposed to have a space at the beginning.") Exit Sub Exit Sub End If Program 4.1b Validating a Full Name

23 2307/10/2015 ‘Len returns the length of the name. The loop ‘is repeated this number of times to process ‘each character in the name. For Index = 1 To Len(FullName) Character = Mid(FullName, Index, 1) ‘Extract one character. Character = Mid(FullName, Index, 1) ‘Extract one character. If Character = “ ” Then ‘Is this character a space? If Character = “ ” Then ‘Is this character a space? Spaces = Spaces + 1 ‘If yes, increment Spaces. End If End If Next Index Program 4.1b Validating a Full Name

24 2407/10/2015 ‘Does the name have more than one space? If Spaces > 1 Then Console.WriteLine(“Too many spaces!”) Console.WriteLine(“Too many spaces!”) End If Program 4.1b Validating a Full Name

25 2507/10/2015 Run the program and test it. Insert spaces in various different locations. Insert spaces in various different locations. Program 4.1b Validating a Full Name

26 Commenting on String Functions For presentations 4.1 & 4.2 I will only ask for comments to string functions.4.14.2 Your comments MUST explain: What does the string function do? Why does this need to be done? When (after and before what) are you doing this and why does it have to be done there? When in the procedure code or, if it is on its own, in which procedure (button, checkbox, textbox, etc…)?

27 2707/10/2015 Extend the program to output an appropriate message if a space has been entered after the surname. Program 4.1b Validating a Full Name

28 Remember – To Search: Use Mid in a loop to extract each character/s in turn from the beginning to end: For Index = 1 To Len(WordToBeSearched) Character/s = Mid(WordToBeSearched, Index, 1) If Character/s = “WhatYouWantToSearchFor” Then Count = Count + 1 ….. End If Next Index

29 2907/10/2015 Extension “Search for letter a” Program 4.1c Write a program that will count the number of letter 'a's in a word. Extension: Extension: Adapt the program so that uppercase 'A' to be counted as well as each lower case 'a‘.

30 3007/10/2015 Extension “Search for text” Program 4.1d Write a program that allows the user to: Enter some text (this could be copied and pasted in). Enter some text (this could be copied and pasted in). Enter a search letter or word (including the word within other words). Enter a search letter or word (including the word within other words). Outputs the number of times this letter or search word is found. Outputs the number of times this letter or search word is found. Hint: In a loop For index = 1 To Len(StringToSearch) For index = 1 To Len(StringToSearch) …… = Mid (StringToSearch, Index, Len(TextToSearchFor)) Extension: Extension: Adapt the program to offer the user a choice of: 1. Counting whole words and words within other words, as above. 2. Counting whole words only.

31 3107/10/2015 Extension “Sort” Program 4.1e Write a program that allows the user to: Enter two single words in two separate text boxes. Enter two single words in two separate text boxes. When a “Sort” button is clicked the 2 words are added to a label with a space in between in alphabetical order. When a “Sort” button is clicked the 2 words are added to a label with a space in between in alphabetical order. Make sure you use the Mid function to examine the letters in each word, one by one. Make sure you use the Mid function to examine the letters in each word, one by one. Make sure you also consider what happens if 2 words are the same but 1 is longer (e.g. apple, apples) and 2 words are exactly the same.

32 3207/10/2015 Extension “Garage” Program 4.1f A garage is having software produced to calculate the bills for its customers. The garage enters details of a job. Duration Parts 01:09 $17.07 02:52 $29.27 04:13 $43.15 The software for the garage includes a function which takes HOURS as a string and returns the number of half hours. For example, if the input is “1:30” the output will be 3; if the input is 2:52 the output will be 6. Continued on the next slide. Continued on the next slide.

33 Extension “Garage” Program 4.1f Here is an algorithm for this function: 33 Write this program. Please note though that you should use 1 input for the “Duration”, not 2, as even though this would be simpler, it avoids using the “String” functions you need to practise here. See the next slide for some hints. See the next slide for some hints.

34 Extension “Garage” Program 4.1f Hints: 1.Look for the colon : using the Instr function to find its position. 2.Use the Mid function to extract the numbers before the : as minutes. 3.Use the Mid function to extract the numbers after the : as hours. Note that if you do not include a second number in the Mid function then Mid will extract all numbers from the position you give, to the end.Note that if you do not include a second number in the Mid function then Mid will extract all numbers from the position you give, to the end. 4.Note that if the minutes is 0 then do not use the minutes at all (just Hours*2). 3407/10/2015

35 3507/10/2015 Plenary How do relational operators ( ) compare strings? > or or < relates to alphabetical order. What do the Instr, Mid and Len functions do? Instr returns the position in the main string of the substring being searched for. Instr returns the position in the main string of the substring being searched for. Mid returns a substring (from a specified location and of a specified length) from the main string. Mid returns a substring (from a specified location and of a specified length) from the main string. Len returns the number of characters in a string. Len returns the number of characters in a string.


Download ppt "07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)"

Similar presentations


Ads by Google