The deadline for this lab is Wednesday 15 November 2006, 12:00:59 hours. Strictly no submissions will be accepted after the deadline.
Write a C program to read in the list of sentences from “sentences.txt” and:
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 testnote: 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!