Strings

A string is a sequence of zero or more characters. Most strings can be named by enclosing the characters they contain between double quotation marks: for instance, "hyperbola" is the nine-character string consisting of the characters #\h, #\y, #\p, #\e, #\r, #\b, #\o, #\l, and #\a, in that order, and "" is the zero-character string (the null string).

Strings may contain spaces and newline characters; when such characters are between double quotation marks, they are treated like any other characters. There is a slight problem when one wants to put a double quotation mark into a string: To indicate that the double quotation mark is part of the string (rather than marking the end of the string), one must place a backslash character immediately in front of it. For instance, "Say \"hi\"" is the eight-character string consisting of the characters #\S, #\a, #\y, #\space, #\", #\h, #\i, and #\", in that order. The backslashes are escape characters, present in the name only to indicate that the characters immediately after them form part of the string.

This use of the backslash character causes yet another slight problem: What if one wants to put a backslash into a string? The solution is to place another backslash character immediately in front of it. For instance, "a\\b" is the three-character string consisting of the characters #\a, #\\, and #\b, in that order.

Scheme provides several basic procedures for working with strings:


Exercise 1

Is the symbol hyperbola a string?


Exercise 2

Is the character #\A a string?


Exercise 3

Does the null string count as a string?



Exercise 4

Suggest two ways of constructing the string ??? -- one using a call to make-string, the other a call to string.



Exercise 5

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

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



Exercise 6

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.)


Exercise 7

Write a Scheme procedure consonant-part that takes one argument, a string, and returns a string similar to its argument except that all of the vowels have been removed. (For instance, the value of (consonant-part "asparagus") should be "sprgs".)


This document is available on the World Wide Web as

http://www.math.grin.edu/~stone/courses/scheme/strings.html

created March 5, 1997
last revised May 31, 1998

Henry Walker (walker@math.grin.edu) and John David Stone (stone@math.grin.edu)