Jerry Cain and Eric Roberts

Slides:



Advertisements
Similar presentations
Chapter 10: Applications of Arrays and Strings J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second.
Advertisements

Code Generator Translator Architecture Parser Tokenizer string of characters (source code) string of tokens abstract program string of integers (object.
1 Chapter Ten Modular Development. 2 Stepwise Refinement At first, simple programs consist of only one source file and one function (the function main.
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
String Processing Eric Roberts CS 106A February 3, 2010.
HW 1: Problems 3 & 4 EC 1 & 2.
It Seemed Like a Good Idea At the Time Suzanne (no last name given) at a university somewhere in the southwest US.
Using addition property of equality
Chapter 8 Characters and Strings. Principle of enumeration Computers tend to be good at working with numeric data. The ability to represent an integer.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Chapter 10 Applications of Arrays and Strings. Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array.
Applications of Arrays (Searching and Sorting) and Strings
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
EXAM 1 REVIEW. days until the AP Computer Science test.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 15 JavaScript.
String Manipulation. Java String class  The String class represents character strings “Tammy Bailey”  All strings (arrays of characters) in Java programs.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Chapter 14: Searching and Sorting
read and learn from example loop programs develop modular program
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
 Problems  Rock-Paper-Scissors (fair game)  Functions Frenzy  Extra Problems  Pig Latin  Scoring Paper.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
String Processing Eric Roberts CS 106A February 1, 2016.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 15 JavaScript.
DEVRY CIS 170 C I L AB 5 OF 7 A RRAYS AND S TRINGS Check this A+ tutorial guideline at
C++ Memory Management – Homework Exercises
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
String Manipulation Part 1
Main Points: - Working with strings. - Problem Solving.
COSC 3340: Introduction to Theory of Computation
Goals Understand how to create and compare Strings.
JavaScript Syntax and Semantics
Ordering of Hypothesis Space
Representing Characters
Characters Computers use the principle of enumeration to represent character data inside the memory of the machine. There are, after all, a finite number.
String Objects & its Methods
Designing and Debugging Batch and Interactive COBOL Programs
Jerry Cain and Eric Roberts
Chapter 7: Strings and Characters
Topics Introduction to File Input and Output
Introduction to C++ Programming
Goals Understand how to create and compare Strings.
Goals Understand how to create and compare Strings.
Linked Lists.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. Every time.
Thinking about Strings
Spring 2010 EECS 110: Homework I.
Chapter 8—Strings and Characters
Algorithm and Ambiguity
Problem Solving Designing Algorithms.
slides courtesy of Eric Roberts and Jerry Cain
Basic String Operations
Goals Understand how to create and compare Strings.
Chapter 4 Unordered List.
AP Java String Review Learning Objectives
Topics Basic String Operations String Slicing
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
Chapter 17 JavaScript Arrays
Topics Basic String Operations String Slicing
Topics Introduction to File Input and Output
CS 100: Roadmap to Computing
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Jerry Cain and Eric Roberts String Processing Jerry Cain and Eric Roberts CS 106J May 3, 2017

String Calisthenics Let’s review some String methods you learned last time: "AEIOUaeiou".length "ABCDEFG".charAt(6) "Harry Potter".indexOf("a") "Harry Potter".indexOf("a", 6) "Harry Potter".lastIndexOf("tt") "bumfuzzle".substring(3, 7) "cabotage".substring(1, 1) "agelast".substring(3) 10 "G" 1 -1 8 "fuzz” "" "last"

Generating Acronyms An acronym is a word formed by taking the first letter of each word in a sequence, as in "North American Free Trade Agreement" "NAFTA" "not in my back yard" "nimby" "self-contained underwater breathing apparatus" "scuba" The text describes and implements two versions of a function acronym(str) that generates an acronym for str: The first version searches for spaces in the string and includes the following character in the acronym. This version, however, fails for acronyms like scuba, in which some of the words are separated by hyphens rather than spaces. The second version looks at every character and keeps track of whether the algorithm is scanning a word formed composed of sequential letters. This version correctly handles scuba as well as strings that have leading, trailing, or multiple spaces.

First Version of the acronym Function str result sp str result sp "Graphics Interchange Format" "GI" "G" "GIF" -1 8 20 > acronym("Graphics Interchange Format") GIF >

Acronym, Take II 11 -1 str result i inWord ch str result i inWord ch "Laughing Out Loud" "LO" "L" "LOL" "" 6 11 5 10 7 9 8 4 17 12 14 1 15 16 2 3 13 true false true true true true false true true true true true true true true true false true "d" "L" "u" "L" "o" "u" "a" "g" "g" "O" " " "u" "n" "i" " " "t" "h" "L" " " > acronym("Laughing Out Loud") LOL

Translating Pig Latin to English Section 6.4 works through the design and implementation of a program to convert a sentence from English to Pig Latin. In this dialect, the Pig Latin version of a word is formed by applying the following rules: 1. If the word begins with a consonant, the wordToPigLatin function moves the initial consonant string to the end of the word and then adds the suffix ay, as follows: scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scr am scram ay If the word begins with a vowel, wordToPigLatin generates the Pig Latin version simply by adding the suffix way, like this: 2. apple apple way If the word contains no vowels at all, wordToPigLatin returns the original word unchanged. 3.

Translating Pig Latin to English "stout plunder lover" i 1 2 3 4 5 6 12 13 14 18 inWord F T T T T T F T T F T T start -1 -1 6 6 -1 14 14 inWord is true if and only if we’re in a word, and start is the index of the first character of the word we’re currently in (or -1 if we’re not in a word). inWord is now true and start is set equal to 0. We set assign the value of i to start at the same time inWord is transitioning from false to true , so we can remember where the current word of interest begins. This is an interesting transition, since the current word we’re in is just now ending. We can isolate the word by calling str.substring(start, i), where str is assumed to be the entire sentence or fragment to be translated. Right now, str.substring(start, i) produces "stout" . And now, str.substring(start, i) produces "plunder" .

Pseudocode for the Pig Latin Program function toPigLatin(str) { Initialize a variable called result to hold the growing string. for (each character position in str) { if (the current character is a letter) { if (we’re not yet scanning a word) Remember the start of this word. } else { if (we were scanning a word) { Call wordToPigLatin to translate the word. Append the translated word to the result variable. } Append the separator character to the result variable. if (we’re still scanning a word) { Call wordToPigLatin and append the translated word to result. function wordToPigLatin(word) { Find the first vowel in the word. If there are no vowels, return the original word unchanged. If the vowel appears in the first position, return the word concatenated with "way". Divide the string into two parts (head and tail) before the vowel. Return the result of concatenating the tail, the head, and the string "ay".

Simulating the PigLatin Program str result start i ch word vp head tail str result start i ch word vp head tail word vp head tail word vp head tail word vp head tail word i ch "isway" "isthay" "igpay" "atinlay" "this is pig latin" "isthay " "isthay isway igpay atinlay" "isthay isway " "isthay" "isthay isway" "" "isthay isway igpay " "isthay isway igpay" 8 -1 5 -1 -1 12 -1 11 14 12 13 15 7 9 10 8 5 2 17 3 4 1 16 6 "t" " " " " "s" "t" "i" "h" "l" "g" "i" "a" "i" "s" "n" "p" " " "i" "is" "latin" "this" "pig" 1 1 2 "l" "th" "p" "this" "atin" "ig" "is" 1 2 "t" > toPigLatin("this is pig latin") isthay isway igpay atinlay

The End