slides courtesy of Eric Roberts and Jerry Cain

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.
Interfaces A Java interface is a collection
©2004 Brooks/Cole Chapter 7 Strings and Characters.
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.
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
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.
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.
Recursion Version 1.0.
String Manipulation Part 1
Main Points: - Working with strings. - Problem Solving.
Jerry Cain and Eric Roberts
Theory of Computation Lecture 12: A Universal Program III
Goals Understand how to create and compare Strings.
JavaScript Syntax and Semantics
Binary Representation
Strings Part 1 Taken from notes by Dr. Neil Moore
Ordering of Hypothesis Space
Characters Computers use the principle of enumeration to represent character data inside the memory of the machine. There are, after all, a finite number.
CS 106A, Lecture 9 Problem-Solving with Strings
Jerry Cain and Eric Roberts
Chapter 7: Strings and Characters
Topics Introduction to File Input and Output
Mechanics of Functions
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
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Coding Concepts (Data- Types)
Algorithm and Ambiguity
Problem Solving Designing Algorithms.
Binary Representation
Basic String Operations
Goals Understand how to create and compare Strings.
slides courtesy of Eric Roberts
AP Java String Review Learning Objectives
Topics Basic String Operations String Slicing
Discovery and Design of Algorithms
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
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
Presentation transcript:

slides courtesy of Eric Roberts and Jerry Cain String Applications Jerry Cain CS 106AJ October 26, 2018 slides courtesy of Eric Roberts and Jerry Cain

String Calisthenics Let’s review some String methods you learned about 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.

acronym , Take I str result sp "not in my back yard" "nim" "nimb" "nimby" "ni" "n" 14 3 9 -1 6 > acronym("not in my back yard") nimby

acroynm, Take II 8 -1 str result i inWord ch ch ch "In My Humble Opinion" "I" "IMHO" "" "IM" "IMH" 14 19 3 4 5 12 6 13 15 7 20 10 18 9 1 17 8 16 2 11 true true true true true true true true true true false true true false false true true true true true false " " "n" "i" "i" "n" "I" "o" "n" "M" "e" "b" "u" "m" "l" " " "H" " " "y" "p" "O" " " "I" > acronym("In My Humble Opinion") IMHO

Translating Pig Latin to English Section 7.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 assign start to be a snapshot of i at the same time inWord transitions 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 word vp head tail word vp head tail word vp head tail word i ch "igpay" "isthay" "isway" "atinlay" "this is pig latin" "isthay " "isthay" "isthay isway igpay " "isthay isway " "isthay isway" "isthay isway igpay" "" "isthay isway igpay atinlay" -1 12 -1 5 8 -1 -1 8 9 2 15 10 1 12 13 4 11 3 7 17 16 14 5 6 " " "n" "l" "t" "t" "i" "g" "i" "i" "s" "a" " " "s" "p" " " "h" "i" "latin" "is" "this" "pig" 1 1 2 "l" "th" "p" "this" "is" "ig" "atin" 1 2 "t" > toPigLatin("this is pig latin") isthay isway igpay atinlay

The End