CS1101C Practical Exam

Session 2 (1015 - 1200 hours)

Sentence Palindrome

The name of your C program must be called palindrome.c. Files with any other name will not be marked. You are not allowed to use structures or dynamic linked lists in your program (you will get zero credit if you do so).

The deadline for this lab is Wednesday 15 November 2006, 12:00:59 hours. Strictly no submissions will be accepted after the deadline.

Background

A Palindrome is a word, phrase or sentence that has the property of reading the same in either direction. For example, “madam” is a palindrome, so is “Yo, banana boy!”. Notice that the case of letters, punctuation marks, spaces and quotes are ignored when considering a palindrome.

Text File

You are provided with a data file called “sentences.txt” (which has been copied into your directory by the pesetup command) that contains a list of sentences.

Write a C program to read in the list of sentences from “sentences.txt” and:

  1. determine if each sentence read is a palindrome or not.

  2. produce a frequency count of distinct words in “sentences.txt”, ignoring case. You do not have to sort the words.

For example, let’s say “sentences.txt” contains the following four lines:

A man, a plan, a canal - Panama!
madam
Helloh!
"Mad at Adam?"

Your program should produce the following results:

Sentence 1: [A man, a plan, a canal - Panama!] is a palindrome.
Sentence 2: [madam] is a palindrome.
Sentence 3: [Helloh!] is NOT a palindrome.
Sentence 4: ["Mad at Adam?"] is a palindrome.

Word     Frequency
A           3
man         1
plan        1
canal       1
Panama      1
madam       1
Helloh      1
Mad         1
at          1
Adam        1

You may assume that there are at most 200 different words, each word is at most 40 characters in length, and each line has a maximum of 70 characters.

Hints:

To read a line of text from a file, use the function fgets (str, n, fptr);

The function prototype for fgets is:

char *fgets (char *str, int n, FILE *fptr);

fgets takes three parameters - the output string (str), a maximum number of characters to store in str (n), and the file pointer to the data file (fptr). fgets will never store more than n - 1 characters from the data file in str, and the final character stored will always be ‘\0’. If fgets has enough room to store the entire line of data, it will include the ‘\n’ character before the ‘\0’. You will have to remove this ‘\n’ character if your line is less than n - 1 characters. When fgets encounters the end of file, the NULL pointer will be returned.

The string library also contains a function strtok that will tokenize a string into tokens separated by a set of supplied delimiters.

The function prototype of strtok is:

char *strtok (char *s1, const char *s2);

s1 is the string to be tokenized, and s2 contains the string of delimiters.

For example, the following code segment will tokenize the following string “Hello, this is a test!” into the string tokens “Hello”, “this”, “is”, “a”, “test”, where the delimiters are ‘,’ (comma), ‘ ’(space) and ‘!’ (exclamation). The order of the delimiters is not important.

char str[80] = "Hello, this is a test!";
char *s;

s = strtok (str, ", !");

while (s != NULL)
{
   printf ("word is %s\n", s);
   s = strtok(NULL, ", !");
}
The output will be:

word is Hello
word is this
word is is
word is a
word is test
note: the first call to strtok will return the first token of the string. Subsequent calls (with first parameter as NULL) will return subsequent tokens. When there are no more tokens, NULL will be returned. strtok will modify the input string (str).

Remember to add the line #include <string.h> since we are using strtok.

We will test your programs with other (more complicated) text input files.

Do not use any structures or dynamic linked lists in your program, else no credit will be given.

Remember to submit your program using the submit palindrome.c command, and check your submission using the check command.

All the best!

UNIX commands

Some useful UNIX commands (in case you forgot what you did in Lab 0):

  1. dir”: lists all the files in the directory.
  2. cp a.txt b.txt”: copies a.txt to b.txt.
  3. mv a.txt b.txt”: moves / renames a.txt to b.txt.
  4. cat a.txt”: shows the contents of a.txt.