Introduction
Welcome to the FiveHive article for topic 2.10 of AP CSA!
In this article, we will briefly cover the learning objective of 2.10 as shown in the AP® Computer Science A Course and Exam Description 2025.
2.10.A Develop code for standard and original algorithms that involve strings and determine the result of these algorithms.
Similar to the last article, this topic covers literally no content to be honest. Here, all CollegeBoard is asking y’all is to apply what you’ve been learning from topic 2.1-2.8 to create algorithms that manipulate/involve strings in some way.
Thus in this article, we’ll mainly do two coding exercises; both you’ll try on your own, but the first one will be an easier one I’ll walk through with you, and the second will be one you do without me holding your and-hay (hand) that is harder, but I’ll give you a canonical solution!
Without further ado, let’s get started. Get excited you guys!
Standard Algorithms (String)
In Java, you can take selection and iteration and smash them to create an algorithm. We focused on algorithms in general last article, but this time we’re placing an emphasis on string algorithms. You can implement algorithms to manipulate strings in various ways, some of which include (CED):
- Find if one or more substrings have a particular property
- Determine the number of substrings that meet specific criteria
- Create a new string with the characters reversed
Today, you’re gonna do the bolded ones, with the first one featuring a quick walkthrough with yours truly!
By the way: OYO = On your own.
Practice: Handheld
“Determine the number of substrings that meet specific criteria”
How about this: let’s write a method that takes a String (word) as a parameter and returns the number of the letter “a” that are present in that word.
For example, the word “apple” would return 1 because it has one a, while purple would return 0 because it has no a’s.
Try this on your own, then come back for my walkthrough.
Did you give it a try? I hope you did! This is for you!
Anyway, let’s knock the obvious write away: we need a method declaration.
public int countLetterA(String word)
{
}
It’s public, meaning anyone can use it, returns an int because we’re counting something, and we take in a String called word.
Then we define an int counter variable starting at 0 that we increment each time we find the letter “a” in our word.
public int countLetterA(String word)
{
int counter = 0;
}
Now, we need a for loop (a while loop works as well, just a for loop is faster) to actually go through each letter of the word. We initialize the header as follows:
-
Set the counter variable of the loop like normal; call it i, letter, whatever you want, and set it to 0. This variable we will use in our substring method to access each letter of the word from start to end.
- Remember, for strings the 0th index is the first letter!
for (int i = 0; ...)-
Then, we make sure to set our boolean expression to increment i so long as its index is less than the word length, avoiding an error.
- Remember, because we start at 0, the last index of the word is always one less than its length.
for (int i = 0; i < word.length(); ...)- Finally, we add our incrementor variable to go through each letter of the word in order.
for (int i = 0; i < word.length(); i++)Then we pop that bad boy into our method!
public int countLetterA(String word)
{
int counter = 0;
for (int i = 0; i < word.length(); i++)
{
}
}
Let’s put our return statement below the loop so that we return counter AFTER going through the word and counting a’s.
public int countLetterA(String word)
{
int counter = 0;
for (int i = 0; i < word.length(); i++)
{
}
return counter;
}
We’re almost done, hang tight!
Now we need our conditional. In it, we have to check if the letter we’re on is a. To do this, we use the substring String method. As you know, it takes two parameters: one for the start of your substring (inclusive), and one for the end of your substring (not included).
With that, to obtain a single letter, we simply put in our first parameter the index of the letter we’re on in the word like so:
.substring(i, ...)And in the second parameter take the index and add one to it, which is the letter following the letter we’re on. Because that letter won’t be in our substring, our final substring will simply be the letter we’re on!
.substring(i, i+1)Now we append it to word
word.substring(i, i+1)And to it, we append to it .equals(“a”) to check if our letter is an “a”.
word.substring(i, i+1).equals(“a”)Wrap it around a conditional
if (word.substring(i, i+1).equals(“a”))Increment counter if the condition is true
if (word.substring(i, i+1).equals(“a”))
{
counter++;
}
And put it in our method, and then we have the final product!
public int countLetterA(String word)
{
int counter = 0;
for (int i = 0; i < word.length(); i++)
{
if (word.substring(i, i+1).equals(“a”))
{
counter++;
}
}
return counter;
}
Practice: OYO
A palindrome is a word that can be written the same forwards AND backwards.
Write two methods (one for each kind of loop) that takes a String (word) as a parameter and determines if it is a palindrome.
The method returns true if the word is a palindrome and false otherwise
For example:
- Passing in the word tacocat should yield: true
- racecar: true
- six seven: false
For Loop Canonical Solution
public boolean palindrome(String word)
{
String reverseWord = “”;
for(int i = word.length() - 1; i >= 0; i--)
{
reverseWord += word.substring(i, i+1);
}
return word.equals(reverseWord);
}
While Loop Canonical Solution
public boolean palindrome(String word)
{
int i = word.length() - 1;
String reverseWord = “”;
while(i >= 0)
{
reverseWord += word.substring(i, i+1);
i--;
}
return word.equals(reverseWord);
}
