Question: Lab 10: String Processing This lab accompanies Chapter 12 of the text book. For this lab, students will work in pairs . Each pair will
Lab 10: String Processing
This lab accompanies Chapter 12 of the text book.
For this lab, students will work in pairs. Each pair will use one computer, one monitor, one mouse, and one keyboard. If questions arise, try to answer them within the pair before turning to another pair for help. Ask the instructor for help as a last resort.
** Download this file from Canvas, type in all the answers, and turn in your lab report online using Canvas. Note that each student needs to submit the report individually in Canvas **
Name#1: Name#2: Date: 11/19/2018
Lab 10.1 Text Processing and Pseudocode
| Critical Review
Most languages provide a way to access the individual characters in a string. Often this is done with subscript notation, similar to accessing the elements of an array.
Pseudocode example: Declare String greeting = "Hello world" Display greeting[0] // display character H Set greeting[6] = W // string is now Hello World
A loop can be used to go through all of the characters in a string:
For index = 0 to length(greeting) 1 Display greeting[i] End For
Most languages also provide away to perform some basic operations on a string such as inserting additional characters, removing some characters, finding characters:
Declare String str1 = "New City" insert(str1, 4, "York ") Display str1 // display New York City
Declare String str2 = "I ate 1000 blueberries!" delete(str2, 8, 9) Display str2 // display "I ate 10 blueberries!"
Declare Boolean found Set found = contains(str2, ate) // found = true
|
This lab examines how to work with a string by writing pseudocode.
Step 1: Complete the pseudocode below for the findChar function according to instructions. This function returns the position/index of the first character found or it returns -1 if character is not found. It really is a sequential search on a list of characters. You might want to use a Boolean variable to stop the loop as soon as the character is found. You need a loop to go through the characters in the string.
Function Integer findChar(Character c, String str)
End Function
Step 2: Declare a String message and initialize it to Hello World!
Step 3: Use the function findChar() above to find character o in String message and assign result to variable position. What value is stored in variable position (there are 2 os in the string and which one does your algorithm find)?
Declare Integer position
Step 4: Use the function findChar() above to find character x in String message and assign result to variable position. What value is stored in variable position?
Declare Integer position
Lab 10.2 Java Code
The goal of this lab is to convert the findChar function from Lab 10.1 to Java code with the help of Java Companions Ch. 12. Create a program which allows the user to input a string and a character, and the program will display the position/index of the first character found or it returns -1.
Program Output
Enter a string: Mr. Jones will arrive TODAY! [Enter] Enter a character: s[Enter]
The position of the first s found is 8.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
