Arrays, Strings, and Bits
- Summary
- You will write programs to compute the Scrabble score of a word, count the letters two strings have in common, and emit a UNIX-style permissions string.
- Objectives
-
- Practice character string and array processing
- Reinforce bit-level data manipulation
Problem 1: Word Scoring
In word board games like Scrabble or Words with Friends, players place letters on a grid to form words. Rules of the games may vary, but a common thread among them is to score each word played as the sum of individual letter scores, with point values inversely proportional to the characters' frequencies.
Write a program wordscore.c that features the function
int wordscore(char *word)
which calculates the total score of the given word according to the following rules:
| Points | Characters |
|---|---|
| 1 | A, E, I, L, N, O, R, S, T, U |
| 2 | D, G |
| 3 | B, C, M, P |
| 4 | F, H, V, W, Y |
| 5 | K |
| 8 | J, X |
| 10 | Q, Z |
Your program should prompt the user to enter a word and report the word's score.
Programming Notes
It is common for null-terminated strings (of indeterminate length)
to be taken as arguments of
type char *. However, in
order to get memory in which to deposit a string entered by a user,
you must declare an array of characters. Remember that an array
variable holds the address of the first element of the array, so it
can be passed as the pointer argument to wordscore.
A functionally equivalent signature for the function would be
int wordscore(char word[])
but this tells the programmer more that the input is an array, rather
than a null-terminated string, so documentation would need to point
out the precondition for null-termination.
Problem 2: Common Letters
Write a program common.c that reads two strings and
counts how many letters the strings have in common. To determine
common letters, each letter of one word should be matched with
exactly one letter of the second word. The case of the letters
(upper case versus lower case) should be ignored. For example:
- "room" and "tool" have two letters in common (each "o" in "room" is matched with a separate "o" in "tool").
- "red" and "fewer" have two letters in common (the "e" in "red" matches one of the "e"s in "fewer" and both words contain one "r").
- "Mississippi" and "Iowa" has just one letter in common (the "I" of Iowa matches one of the "i"s in "Mississippi").
Programming Notes
The standard C header
file <ctype.h> contains the
definition of many useful character functions.
Problem 3: Permission Bits
As you may recall from our earlier
reading on
Linux file permissions, three categories of permissions are
granted to three categories of users. These nine bits are packed
together into a single integer; the standard system header
file <sys/stat.h> features bit masks allowing
programmers to extract the individual bits, as shown in the following
table.
| Bit mask | Meaning |
|---|---|
S_IRUSR | user-read |
S_IWUSR | user-write |
S_IXUSR | user-execute |
S_IRGRP | group-read |
S_IWGRP | group-write |
S_IXGRP | group-execute |
S_IROTH | other-read |
S_IWOTH | other-write |
S_IXOTH | other-execute |
