Download presentation
Presentation is loading. Please wait.
Published byMarkus Knudsen Modified over 6 years ago
1
4.1 Strings ASCII & Processing Strings with the Functions
- Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc) 31/12/2018
2
Learning Objectives Explain how relational operators (< >) compare strings. Explain what the Instr, Mid and Len functions do. 31/12/2018
3
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 31/12/2018
4
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. 31/12/2018
5
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. – 2 – 1 ….. – A – B 31/12/2018
6
Representing Characters and Numbers
e.g. If the ‘A’ key is pressed ‘ ’ is sent to the CPU. If the 1 key is pressed then ‘ ’ is sent to the CPU. If the user wants to print ‘123’ the codes for 1, 2 & 3 are sent to the printer. 31/12/2018
7
Processing Strings 31/12/2018
8
Using relational operators
> or < relates to 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(Name2) Else End If 31/12/2018
9
How ASCII codes are used to arrange letters in alphabetical order
Letters 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. 31/12/2018
10
4.1a ASCII Specification:
Allow the user to convert a letter to its corresponding ASCII code and vice versa. 31/12/2018
11
Program 4.1a ASCII Console.WriteLine(“Enter a letter.”)
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) 31/12/2018 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) 31/12/2018
13
Program 4.1a ASCII Run the program and test it. 31/12/2018
14
Searching for a substring using Locate (Instr)
The Instr function returns the position in the main string of the substring being searched for. Assume declaration of variables below. MainString = “The man looked up and saw the man in the moon.” SearchString = “man” 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”. 5 31/12/2018
15
Processing individual characters using Mid
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) How many characters. Position l 31/12/2018
16
Processing individual characters using Mid
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) How many characters. Position lo 31/12/2018 16
17
Length (Len) 6 Len returns the number of characters in a string.
Dim EmployeeName As String Dim LengthOfString As Integer EmployeeName = “Mr Lee” LengthOfString = Len(EmployeeName) MsgBox(“The length of the Employee’s name is: “ & LengthOfString) 6 31/12/2018
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: If Position = 2 Then If Character = “b” Then If LengthOfString >=5 Then You can also mix these functions: 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.
19
Program 4.1b Validating a Full Name
Specification: Ask the user to enter a person’s surname and then their first name. 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. 31/12/2018
20
Program 4.1b Validating a Full Name
Create a new project named ‘Validating a Full Name’. 31/12/2018
21
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 31/12/2018
22
Program 4.1b Validating a Full Name
‘Is there a space at the beginning (1st position)? If InStr(FullName, " ") = 1 Then Console.WriteLine(“You are not supposed to have a space at the beginning.") Exit Sub End If 31/12/2018 22
23
Program 4.1b Validating a Full Name
‘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. If Character = “ ” Then ‘Is this character a space? Spaces = Spaces + 1 ‘If yes, increment Spaces. End If Next Index 31/12/2018 23
24
Program 4.1b Validating a Full Name
‘Does the name have more than one space? If Spaces > 1 Then Console.WriteLine(“Too many spaces!”) End If 31/12/2018 24
25
Program 4.1b Validating a Full Name
Run the program and test it. Insert spaces in various different locations. 31/12/2018
26
Commenting on String Functions
For presentations 4.1 & 4.2 I will only ask for comments to string functions. 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 code?
27
Program 4.1b Validating a Full Name
Extend the program to output an appropriate message if a space has been entered after the surname. 31/12/2018
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
Extension “Search for letter a” Program 4.1c
Write a program that will count the number of letter 'a's in a word. Extension: Adapt the program so that uppercase 'A' to be counted as well as each lower case 'a‘. 31/12/2018
30
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 a search letter or word (including the word within other words). Outputs the number of times this letter or search word is found. Hint: In a loop For index = 1 To Len(StringToSearch) …… = Mid (StringToSearch, Index, Len(TextToSearchFor)) 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/12/2018
31
Extension “Sort” Program 4.1e
Write a program that allows the user to: Enter two single words separately. Then the 2 words are displayed, 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 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. 31/12/2018
32
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. 31/12/2018
33
Extension “Garage” Program 4.1f
Here is an algorithm for this function: 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. Also note that the calculation shown in the flowchart is actually not quite correct! Can you see why? See the next slide for some hints.
34
Extension “Garage” Program 4.1f
Hints: Look for the colon : using the Instr function to find its position. Use the Mid function to extract the numbers before the : as minutes. 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 the minutes is 0 then do not use the minutes at all (just Hours*2). If Minutes is 1-30 then add …. , if Minutes = then add …. . 31/12/2018
35
Plenary How do relational operators (< >) compare strings?
> 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. 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. 31/12/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.