4.1 Strings ASCII & Processing Strings with the Functions

Slides:



Advertisements
Similar presentations
30/04/ Selection Nested If structures & Complex Multiple Conditions.
Advertisements

1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Data Representation (in computer system) Computer Fundamental CIM2460 Bavy LI.
CHARACTERS Data Representation. Using binary to represent characters Computers can only process binary numbers (1’s and 0’s) so a system was developed.
08/09/ Arrays Defining, Declaring & Processing.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
Data Representation S2. This unit covers how the computer represents- Numbers Text Graphics Control.
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
19/10/20151 Data Structures Arrays. 219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s.
Manipulating Text In today’s lesson we will look at: why we might want to pick out parts of text strings some BASIC functions that can be used to chop.
30/10/ Iteration Loops Do While (condition is true) … Loop.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
Data Representation Conversion 24/04/2017.
22/11/ Selection If selection construct.
Introduction to Unix (CA263) File Editing By Tariq Ibn Aziz.
Validation final steps Stopping gaps being entered in an input.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
02/03/ Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
09/06/ Data Representation ASCII, Binary Denary Conversion, Integer & Boolean data types.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Do-While Loops A do-while loop is always guaranteed to iterate at least once. This is because the condition is evaluated at the end of the loop, instead.
Nat 4/5 Computing Science Data Representation Lesson 3: Storing Text
Binary Representation in Text
Binary Representation in Text
Sorts, CompareTo Method and Strings
Whatcha doin'? Aims: To start using Python. To understand loops.
Strings CSCI 112: Programming in C.
COMPUTATIONAL CONSTRUCTS
Programming Mehdi Bukhari.
IF statements.
Guide To UNIX Using Linux Third Edition
Engineering Innovation Center
Representing Characters
IDENTIFIERS CSC 111.
CS1S467 GUI Programming Lecture 3 Variables 2.
Manipulating Text In today’s lesson we will look at:
Encryption and Decryption
Data Representation Conversion 05/12/2018.
If selection construct
Do While (condition is true) … Loop
If selection construct
Do … Loop Until (condition is true)
1D Arrays Defining, Declaring & Processing
Coding Concepts (Data- Types)
Text / Serial / Sequential Files
3.1 Iteration Loops For … To … Next 18/01/2019.
Nested Loops & The Step Parameter
Python programming exercise
Random Access Files / Direct Access Files
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Flowcharts and Pseudo Code
Learning Intention I will learn how computers store text.
Introduction to Computer Science
23/04/2019 Data Representation Conversion.
Chapter 4: Repetition Structures: Looping
Just Basic Lessons 9 Mr. Kalmes.
ASCII LP1.
10.3 Procedures Function Procedures 07/06/2019.
Text / Serial / Sequential Files
The Step Parameter & Nested For … To … Next loops
3.2 Working with Data Scope of variables 29/07/2019.
8 Records 25/07/2019.
Chapter 3 - Binary Numbering System
ASCII and Unicode.
Switch Case Structures
Presentation transcript:

4.1 Strings ASCII & Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc) 31/12/2018

Learning Objectives Explain how relational operators (< >) compare strings. Explain what the Instr, Mid and Len functions do. 31/12/2018

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

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

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. 0110010 – 2 0110001 – 1 ….. 1000001 – A 1000010 – B 31/12/2018

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 user wants to print ‘123’ the codes for 1, 2 & 3 are sent to the printer. 31/12/2018

Processing Strings 31/12/2018

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

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. https://simple.wikipedia.org/wiki/ASCII 31/12/2018

4.1a ASCII Specification: Allow the user to convert a letter to its corresponding ASCII code and vice versa. 31/12/2018

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.

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

Program 4.1a ASCII Run the program and test it. 31/12/2018

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

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

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

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

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.

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

Program 4.1b Validating a Full Name Create a new project named ‘Validating a Full Name’. 31/12/2018

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

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

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

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

Program 4.1b Validating a Full Name Run the program and test it. Insert spaces in various different locations. 31/12/2018

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?

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

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

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

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

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

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

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.

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 = 31-59 then add …. . 31/12/2018

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