| 1. |
For this lab you will write several string processing functions.
- Create an HTML file for this lab assignment called lab8.html
(See the instructions for Lab 2 if you are unsure how to do this.)
|
| 2. |
String Copy
|
| 3. |
String Reverse
- You may have notice that our CopyString function doesn't do anything very interesting.
- Write a function called Reverse that will take a string as an argument and return the reverse of the string.
- For example: Reverse( "Evian" ) should return the string "naivE".
- Hint: this function is very similar to CopyString, except for the concatenation part.
- Add a new button labelled Reverse that reverses the string in your input text box and stores the result in the output text box
|
| 4. |
Stripping out Spaces and Punctuation
-
The following code will strip out all of the spaces and punctuation from a string called phrase:
index = phrase.search( /[ !?.,;:='"]/ );
while( index != -1 ) {
phrase = phrase.substring( 0, index ) +
phrase.substring( index + 1, phrase.length );
index = phrase.search( /[ !?.,;:='"]/ );
}
-
Some things to note about this code:
- The first line uses the search function to find the first space or punctuation mark in the phrase.
- The jumble of characters between the slashes is called a regular expression, and indicates which characters to look for.
- The index variable is used to store the position of the first space or punctuation mark.
- The first line in the body of the loop uses the substring function to make a new string without the character at index.
- It does this by concatenating all of the characters before the index to all of the characters after the index.
- The second line of the loop finds the position of the next space or punctuation mark
- The loop continues until index is -1, which means that no more spaces or punctuation marks have been found.
- Write a function called Strip that uses the above code to strip the spaces from a string.
-
Add a new button labelled Strip that strips the spaces and punctuation marks from the string in your input text box
and stores the result in your output text box.
|
| 5. |
Palindromes
|
| 6. |
When you are done, upload the lab8.html file to the CS server using FileZilla.
(See the instructions for Lab 1 if you are unsure how to do this)
|