1. Building strings
- preliminaries
- removing characters
- swapping characters
Preliminaries
We looked at a few examples on manipulating strings where the goal was to create
a modified copy of the input string. Common tasks that create modified copies of
the input string include removing a character from a string, replacing a character
in a string with a new character, etc.
Our approach was to build the result strings one character at a time. In Java
the + operator can be used to append/prepend a character to a string:
Example of APPENDING a character to a string
String word = ""; // word is the empty string
word = word + "n"; // word is n
word = word + "o"; // word is no
word = word + 'w'; // word is now
Example of PREPENDING a character to a string
String word = ""; // word is the empty string
word = 'n' + word; // word is n
word = 'o' + word; // word is on
word = 'w' + word; // word is won
Removing a character
Goal: Write a procedure removeConsonants( String phrase ) which returns a copy
of the given phrase but with all consonants removed.
For example,
removeConsonants( "hello" ) returns eo
removeConsonants( "aio" ) returns aio
removeConsonants( "gbcs" ) returns ???
The general strategy here is to start with a blank result string and go through
the original string. Every time the current character in the original string
is not cosonant we append the current character to the result string.
Here is the outline in English:
Outline:
1. create a variable to keep track of the result string -- initially empty
2. go through original string one index at a time:
3. if current character IS NOT CONSONANT:
append current character to result
4. return result string
Here is the translation in Java:
String removeConsonants( String phrase )
{
String result = "";
for ( int index = 0 ; index < phrase.length() ; index = index + 1 )
{
char curLetter = phrase.charAt( index );
if ( isConsonant( curLetter ) == false ) // if NOT consonant:
{
result = result + curLetter; // keep it
}
}
return result;
}
Swapping pairs of characters
Goal: Write a procedure swapPairs(String phrase) which returns a copy of the
given phrase such th every pair of neighboring characters has been swapped.
For example,
swapPairs( "help" ) returns ehpl
swapPairs( "helloworld" ) returns ehllworodl
For this problem the index should jump by 2, so that we can process a pair of
characters at a time -- extract the current character and its neighbor, glue
them in correct order onto the result, and jump to the next pair.
Here is the outline in English:
Outline:
1. create a variable to keep track of the result string -- initially empty
2. go through original string jumping by 2 each time:
3. extract current character (at current index)
4. extract next character (at next index, i.e. index + 1)
5. append the two characters to the result
(first append the second character, then append the first character)
6. return result string
Here is the translation in Java (only works for even length strings):
// this version only works for even length strings
String swapPairs( String phrase )
{
String result = "";
for (int index = 0 ; index < phrase.length() ; index = index + 2)
{
char curLetter = phrase.charAt( index ); // (Step 3)
char nextLetter = phrase.charAt( index + 1 ); // (Step 4)
result = result + nextLetter + curLetter; // (Step 5)
}
return result;
}
Ignoring the last symbol (the last index)
The procedure above only works for strings of even length. If the string has
odd length the last character will not have a neighbor and the program will
"crash", since we will try to access an invalid index.
For example, if the input string is "hello", the index will "step" on the
following characters:
h e l l o
^ ^ ^ index jumps by 2 each time;
| | | last index does not have neighbor
To solve this problem, we can set up the loop so that it does not allow the
index to "step" on the last character. Since the index of the last character
is length()-1 we can modify the loop condition to index < phrase.length() - 1:
String swapPairs(String phrase)
{
String result = "";
for (int index = 0 ; index < phrase.length() - 1 ; index = index + 2)
{
char curLetter = phrase.charAt( index ); // (Step 3)
char nextLetter = phrase.charAt( index + 1 ); // (Step 4)
result = result + letter2 + letter1; // (Step 5)
}
return result;
}
The new version does not allow the index to "reach/step" on the last character,
so the procedure will only visit the following characters:
h e l l o
^ ^ ^ index jumps by 2 each time;
| | x last index not allowed by loop condition
This also means that the last character will not be included in the final result.