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 wordscorer.c that features the function

int wordscore(const 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

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:

To accomplish this, your program must make the calculation via the helper function

int common_chars(const char* word1, const char* word2)
which accepts the two null-terminated strings and returns the number of letters the strings have in common.

Programming Notes

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

For example, the bitmask S_IRUSR has the binary value (spaces inserted for readability) 100 000 000, that is a one in the leftmost of nine positions, while S_IWGRP would be 000 010 000.

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

void get_permissions(unsigned short perms, char output[9])

Then use the procedure in a main program that tests your procedure by printing out the permissions in a form similar to ls -l.

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

Note: The parameter perms should have no bits to the left of position nine set. Your get_permissions procedure should verify this with an assertion.

Hint: Think about the numeric range of valid inputs.

Programming Notes

Grading

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

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