CSC 153 Grinnell College Spring, 2005 Computer Science Fundamentals Laboratory Exercise

# Character Data

## Goal

This laboratory exercise provides experience working with character data in Scheme.

## Reference

Throughout this lab, you may want to refer to the Revised (5) report on the algorithmic language Scheme for information on various procedures involving characer data.

## Character Data and Coding

1. Determine the character codes for various characters, including:
```
a
A
b
B
G
;
]
0
1
5
9
```
1. Are the character codes for uppercase letters the same as those for lowercase letters?
2. How are the character codes for digits related?
2. Use the comparison

```
(char<? #\a #\b)
```

to check whether the character a comes before the character b.

3. Use various comparison operations to determine whether an uppercase A comes before, comes after, or is equal to a lowercase a. Similarly, use these character comparison operations to confirm the relative orders of the characters from step 1.

4. Compare the case-insensitive versions of the comparison with various combinations of characters, including uppercase letters, lowercase letters, punctuation, and digits. For example, what are the results of the following?

```
(char-ci<? #\0 #\A)
(char-ci<? #\0 #\a)
```

Try with several other pairs of characters. Conclude your experiments with the following:

```
(char<? #\] #\A)
(char<? #\] #\a)
(char-ci<? #\] #\A)
(char-ci<? #\] #\a)
```

5. Consider the following procedure that counts the number of times the letter A appears within a string of characters in either uppercase or lowercase form:

```
(define count-As
(lambda (str)
;Pre-condition:  str is a character string
;Post-condition:  returns number of As in str
(count-As-kernel (string->list str))
)
)

(define count-As-kernel
(lambda (ls)
;Pre-condition:  ls is a list of characters
;Post-condition:  returns number of As in ls, ignoring case differences
(cond ((null? ls) 0)
((char-ci=? #\a (car ls)) (+ 1 (count-As-kernel (cdr ls))))
(else (count-As-kernel (cdr ls)))
)
)
)
```

Check that count-As works by testing it with several strings, including
(count-As "This is a string with 2 A's").

6. Define a procedure vowel? which has a single parameter and which returns true if the parameter's value is a vowel and false otherwise.

7. The following procedure illustrates some code that has been turned in the past few semesters by students. While the code works, it may be simplified significantly. Write equivalent code which is as simple as possible:

```
(define compare?
(lambda (ch1 ch2)
;Pre-condition:  ch1 and ch2 are characters
;Post-condition:  returns true if character ch1 comes before character ch2
(if (< (char->integer ch1) (char->integer ch2))
#t
#f
)
)
)
```
8. Modify count-As to obtain a procedure count-vowels which determines the number of vowels in a string.

9. Modify count-As and count-As-kernel, so that count-As-kernel is tail recursive.

10. Refer to the Revised Report (5) to identify other character procedures that are built into Scheme. Then, modify count-As to count the number of uppercase letters that appear in a string.

11. (Optional, and a bit more of a challenge) Modify count-As to obtain a procedure count-punc which determines the number of punctuation marks within a string. [For this problem, you first may need to decide just what should be considered as a punctuation mark.]

This document is available on the World Wide Web as

```http://www.cs.grinnell.edu/~walker/courses/153.sp07/labs/lab-characters.shtml
```