# FRQ 1B - Strings - Part 2

Each coding exercise has its own folder. Open its `.java` file and press **Run** to try it. **Run Tests** checks every graded exercise in this lesson at once.

From the 2025 Course and Exam Description:

**Free-Response Question 1: Methods and Control Structures**. Students will write two methods or one constructor and one method of a given class based on the provided specifications and examples. In Part A (4 points), the method or constructor will require students to write iterative or conditional statements, or both, as well as statements that call methods in the specified class.

In Part B (3 points)****, the method or constructor will require calling String methods.

FRQ 1B requires the following skills:

- Complete a method or a constructor
- Call String methods
- Use if statements
- Use a for loop or a while loop

Teacher Insights

- You do not need to include import statements. You can assume that any standard Java class is already imported.
- You will NEVER print. If you think you need to print, you probably need to return something.
- You will NOT need to write a main.

## String Algorithms

There are many algorithms that traverse or process Strings that use loops and conditional statements.

Common algorithms include:

- Counting the number of substrings that meet specific criteria
- Determining if one or more substrings has a specific property
- Building a new String based on certain conditions

## Processing a String with a For Loop

Many algorithms can be solved with the accumulator pattern, similar to FRQ1A. In some cases, the accumulator value will be a String.

There are four steps to the accumulator pattern:

- initialize the accumulator variable before the loop
- loop through the letters of the String
- update the accumulator variable inside the loop
- return the accumulated value (if necessary)

## String Algorithm - Count Vowels

### Exercise 1

You are working on a text-analysis tool. The StringProcessor class contains a helper method, isVowel(String letter), which returns true if a single-character string is a vowel (A, E, I, O, U, case-insensitive) and false otherwise.

Write the countVowels method. This method analyzes a given String and returns the total number of vowels present in that string.

> Open the `01-frq1b2-countVowels` folder and edit `StringProcessor.java`. Press **Run** to try it; **Run Tests** checks it (5 tests).

## String Algorithm - Make Abbreviation

### Exercise 2

Write a method called makeAbbreviation that takes a multi-word String and returns a new String consisting of the first letter of each word in uppercase.

> Open the `02-frq1b2-makeAbbreviation` folder and edit `Abbreviator.java`. Press **Run** to try it; **Run Tests** checks it (5 tests).

## String Algorithm - Mask Vowels

### Exercise 3

Write a method called maskVowels that takes a String and returns a new String where all vowels are replaced with asterisks (*), but only if the String is longer than 3 letters.

> Open the `03-frq1b2-maskvowels` folder and edit `WordTransformer.java`. Press **Run** to try it; **Run Tests** checks it (5 tests).

## String Algorithm - Count Occurrences

### Exercise 4

Write a method called countOccurrences that determines how many times a specific target string appears within a larger text string.

Be careful! It is easy to end up with an infinite loop if you are not careful when updating your loop variable. You will need to use the indexOf method to find the next occurrence of the target string, and then update your loop variable to start searching for the next occurrence after the current one.

> Open the `04-frq1b2-countOccurrences` folder and edit `PhraseAnalyzer.java`. Press **Run** to try it; **Run Tests** checks it (5 tests).
