8.  Strings; Strings as Lists:  Homework

Last revision:  January 26 1998

Author: YourNameGoesHere

Creating Your Own String Functions

1  Imitating StringLength, StringTake, StringDrop, StringReverse

In this section you learned about the functions "StringLength[]", "StringTake[]", "StringDrop[]", and "StringReverse[]".  Define your own functions which will do the same thing without using any String functions except "Characters[]" to convert a String to a list of characters, and "StringJoin[]" to put the String back together.  Remember to try the operations you think you need on a sample String, when you're convinced define a function, and then test it out!

2  Imitating StringInsert

There is Mathematica function "Insert[]" which takes a list, an expression, and an integer as arguments.  Find out what "Insert[]" does, compare this with what "StringInsert[]" does, and define your own function which accomplishes what "StringInsert[]" does without using any String functions except "Characters[]" to convert a String to a list of characters, and "StringJoin[]" to put the String back together.  (Depending on your method, you may need to use "Flatten[]", but you may not need to.)

3  Sorting characters in a string

We learned that "Sort[]" will sort a list of strings into some order.  But what about sorting the characters in one string?  Define a function which takes a string, sorts its characters, and returns a string containing the characters in the new sorted order.

4  Capital ideas

We learned that "ToUpperCase[]" takes a String and replaces every letter in it by its upper-case equivalent.

(a) Define a function which takes a String and returns the same string with only the first letter changed to upper-case.  For instance, upperFirst["oswald"] should return "Oswald".

(b) Now define a function which takes a String and a number and returns the same String with the letter indexed by the number coverted to upper-case.  For instance, upperN["NASa",4] should return "NASA".

5  Another string-to-number conversion function

Define a function which takes a String of numbers which includes a decimal point (".") and converts it to its numeric value.  For instance, "newString2Num["23.5"]" should return the numeric value 23.5.  Hint:  Use "Position" to find the decimal point's position in the list of characters in the string; use "string2Num[]" (from the section) to convert the characters preceding the decimal point into an integer, and use "string2Num[]" again on the characters following the decimal point, dividing by the appropriate power of ten.