find if one or more substrings has a particular property
determine the number of substrings that meet specific criteria
create a new string with the characters reversed
LOOPS AND STRINGS
Loops are often used for String Traversals where the code steps through a string character by character.
We have already used String objects and built-in string methods to process strings:
int length() returns the number of characters in a String object.
int indexOf(String str) returns the index of the first occurrence of str; returns -1 if not found
String substring(int from, int to) returns the substring beginning at index from and ending at index (to -1). Note that s.substring(i, i + 1) returns the character at index i.
String substring(int from) returns the substring(from, length()).
In this lesson, we will write our own loops to process strings.
Remember that strings are a sequence of characters where each character is at a position or index starting at 0.
Notice that the first character in a String is at index 0 and the last character is at length() - 1.
The substring method will be used a lot in this lesson, so let's play the String-Thing game a little bit to remind ourselves how it works:
FIND AND REPLACE LOOP
A while loop can be used with the String indexOf method to find certain characters in a string and process them, usually using the substring method.
For example, the following code finds and removes all of the a's in a string:
Google has a project (I'm not sure if they still do) where they scan old books and the used software to read the scanned text.
But, the software could get things mixed up, for example, mixing up 1's with l's.
The following code loops through a string replacing all 1's with l's.
Notice that indexOf here can work repeatedly to find the next occurrence of 1 because they are replaced as soon as they are found.
FOR LOOPS TO PROCESS STRINGS
While loops are often used when you do not know exactly how many times the loop needs to run (like looking for an unknown number of errors in a string).
For loops can be used to process strings when you know you will need to visit every character.
For loops with strings usually start at 0 and use the strings length() for the ending condition to step through a string character by character.
For example, the following code will count the number of e's in a string:
Here is a for loop that creates a new string that reverses the string s.
We start with a blank string sReversed and build up our reversed string in that variable by copying the characters from the string s:
SUMMARY
Loops can be used to traverse or process a String.
There are standard algorithms that utilize String traversals to:
Find if one or more substrings has a particular property
Determine the number of substrings that meet specific criteria
Create a new String with the characters reversed
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) I use the "Find and Replace" tool in various word processors and even while coding in Repl.it all the time! In Repl.it or Google docs I hit ctrl + f on the keyboard, click the little arrow or more button to toggle find and replace mode, type in what I want to find and what I want to replace it with, then I usually click the "Replace All" button.
Your task is to create a "Find and Replace" tool program. To accomplish this you will need to:
import java.util.Scanner;
Your program MUST function using the substring method as shown in the lesson (look at the program where I replaced 1's with l's).
I recommend creating a string variable with a message that contains several of the same word for testing (and printing it out for the user to see the original message).
Use the scanner to get input for the user. I recommend having a string variable named "find" and another string variable named "replace".
Modify the find and replace while loop shown above to find the word the user types in first and replace it with the word they type in second.
I created a program that meets the requirements for this objective! This is the output in the console: