lecture6slides

lecture6slides - Today Lets take a step back for a second...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Today Let’s take a step back for a second! Those of you that have never programmed before are having problems with Project #3. In class, we’ve been learning lots of syntax, but we need to take a step back and learn how to use this syntax to program. 1. Let’s do Project #3 replace 2. ASCII revisited 3. More functions 4. Reference parameters 5. Default parameters 6. Function Overloading
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Project #3: Replace Let’s use top-down, stepwise refinement to solve this problem: GOAL: A program that replaces one or more occurrences of a source phrase in a sentence with a target phrase . Assume the user has already entered the sentence. REFINEMENT: 1. Ask the user to input the source and target phrases. 2. Determine how many times the source phrase occurs in the sentence. 3. Display a prompt asking the user which occurrence they want to replace, or replace all of them. 4. If the user selected (A)ll, then replace all occurrences. 5. If the user selected a specific occurrence, then replace that occurrence.
Background image of page 2
REFINEMENT 2: Determine how many times the source phrase occurs in the sentence. 1. Reset a count variable to 0. 2. Reset an offset variable to 0. 3. Use the string.find function to find the first occurrence of the source string in the sentence, starting at offset . (we learned this 1 week ago!) 4. If the source string was found: A. increment count by one B. set offset to the location in the string where source was found. C. Go back to step 3 5. If source string was not found, we’re done.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
the user. .. 1. If there is only one occurrence of the source string in our sentence (count == 1) then do not display a prompt. 2. If there are more than one occurrence of source string in our stentence, then: A. Display “Replace all (A) or just one (” B. Iterate from 1 to count and print out each number. C. Display “)?” D. Prompt the user for their reply in a string. E.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/08/2011 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 20

lecture6slides - Today Lets take a step back for a second...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online