# Characters and Strings

## Exercises

### Exercise 0: Preparation

If you have not done so already, you may want to skim the reading on chacters and strings. You do not need to read the appendix right now, but should do so eventuallly.

### Exercise 1: Collating Sequences

a. Determine the ASCII collating-sequence numbers for the capital letter A and for the lower-case letter a.

b. Find out what ASCII character is in position 38 in the collating sequence.

c. Do the digit characters precede or follow the capital letters in the ASCII collating sequence?

d. If you were designing a character set, where in the collating sequence would you place the space character? Why?

e. What position does the space character occupy in ASCII?

### Exercise 2: A Control Predicate

In ASCII, the collating-sequence numbers of the control characters are 0 through 31 and 127. Define a predicate `char-control?` that returns `#t` if its argument is a control character, `#f` otherwise.

### Exercise 3: String Basics

a. Is the symbol `hyperbola` a string?

b. Is the character `#\A` a string?

c. Does the empty string count as a string?

### Exercise 4: Creating Questions

Suggest three ways of constructing the string `???` -- one using a call to `make-string`, one a call to `string`, and one a call to `string->list`.

### Exercise 5: Converting to Uppercase

a. Define and test a procedure named `list-upcase` that takes a list of characters as its argument and returns a similar list of characters, but with all of the lower-case letters converted to upper case.

```> (list-upcase (list #\c #\a #\t))
(#\C #\A #\T)
> (list-upcase (list #\" #\W #\h #\a #\t #\? #\"))
(#\" #\W #\H #\A #\T #\? #\")
> (list-upcase null)
()
```

b. Using `list-upcase`, write a corresponding `string-upcase` that converts a string to all uppercase.

### Exercise 6: Removing Whitespace

a. Define and test a procedure named `remove-whitespace` that takes a list of characters as its argument and returns a similar list of characters, excluding all whitespace characters. (The newline, the tab, and the space are the primary whitespace charactrers.)

b. Define a similar procedure that takes a string as an argument and returns the string with all the whitespace chaacters removed.

c. Define a similar procedure that can take a list or a string as an argument and returns the appropriate type of result (a list if given a list; a string if given a string).

### Exercise 7: Referencing Lengths

Here are two opposing views about the relationship between `string-length` and `string-ref`:

• ``No matter what string `str` is, provided that it's not the null string, `(string-ref str (string-length str))` will return the last character in the string.''
• ``No matter what string `str` is, ```(string-ref str (string-length str))``` is an error.''

Which, if either, of these views is correct? Why?

### Exercise 8: Tallying Vowels

Write a Scheme procedure `tally-vowels` that takes one argument, a string, and determines how many characters in that string are vowels. (For this purpose, you should count only the ten characters `#\a`, `#\e`, `#\i`, `#\o`, `#\u`, `#\A`, `#\E`, `#\I`, `#\O`, and `#\U` as vowels.)

Do not use `string->list` in writing this procedure.

## History

Tuesday, 3 October 2000

• Created.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.