CSC 161 Schedule Readings Labs & Projects Homework Deadlines Resources

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:

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.

The nine file access permission bits, from <sys/stat.h>.
Bit maskMeaning
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
Figure 4.4 from W. Richard Stevens, Advanced Programming in the UNIX® Environment (Addison-Wesley).

Using these definitions, write the following procedure that takes an integer and fills in the appropriate slots in a permissions string.

void get_permissions(int perms, char output[9])

Use the procedure in a program permissions.c that prints out permissions in a form similar to ls -l.

get_permissions(S_IRUSR | S_IWUSR | S_IRGRP, ... );
rw-r-----
get_permissions(0755, ... );
rwxr-xr-x
printPerms (0664, ... );
rw-rw-r--

Programming Notes

Grading

In addition to the general grading guidelines for evaluation, the assignment is worth 25 points.

A five point penalty will apply to any submission that includes personally identifying information anywhere other than the references/honesty file.