String Processing
In this chapter, we will combine everything we have learned about Strings and characters so far. You will become more familiar with the underlying patterns involved in processing Strings. By using these patterns, you will learn how to do more advanced forms of String processing.
String Processing Pseudocode
Pseudocode is a step-by-step description of what you want a program to do in plain English. Before writing any actual code, it is always recommended you write pseudocode first. This helps us figure out how to structure more complex programs without getting bogged down in specifics.
By now, you should be familiar with how to loop over Strings. Looping over Strings is the most common form of String processing. You will consistently find yourself having to write methods which do something to each of the characters in a String. This follows a pattern, and that pattern can be expressed in the pseudocode given below:
We can use this as a guideline to write many different types of String processing methods. Take this as an example:
The relevant pseudocode for each line of actual code is given on the right. Let's test our method to make sure it works:
We have added a run method to test String input from the user. Here is an example of what a user might type as a test String after running the program:
You are not limited to simply storing each character into a new resulting String. You can do anything to each of the characters. You could change the case, change the ordering, or even change each of the characters into a different character entirely. The possibilities are endless with how you choose to process the String!
Finding Palindromes
Let's look at a more advanced example of String processing. In this example, we will write a program which checks to see if a given String is a palindrome. A palindrome is a word or sentence that is the same forwards and backwards. Here are a some examples of palindromes:
Writing an isPalindrome
Method
isPalindrome
MethodLet's use top-down design and pseudocode to help us solve this problem. In order for a String to be a palindrome, we know that it must:
Be the same forwards and backwards.
Be equal to the reversed version of the String.
Let's write a method in pseudocode which checks for this:
First, we want to reverse the String, so we will have to write another method to do that. Then, if the original String is equal to the reversed String, we know that it must be a palindrome. Our method will return true. If the original String is not equal to the reversed String, we know that it is not a palindrome. Our method returns false instead.
Writing a reverse
Method
reverse
MethodNow that we know we need a reverse method, we can write the pseudocode for that:
Does this look familiar? It should! We are using the same general pattern for processing a String that was given at the start of this chapter!
First, we need to create a result String. We then take a look at each character starting from the end of the String. We add each of these characters into the result String. We finish by returning the resulting reversed String.
Putting It All Together
With both methods written in pseudocode, we have a good outline for our program:
We just need to fill in the details!
In our run method, we get a String from the user. We then check to see if it is a palindrome by calling the isPalindrome
method. Since we have the pseudocode already written for this method, we just need to convert it into actual code. Once we have that method finished, we can do the same for the reverse
method.
Let's test it with a palindrome!:
Now let's test it with a word that is not a palindrome:
By using good top-down design principles and writing out pseudocode for each of our methods, we have created a very advanced String processing program.
Last updated