CS113: Homework 3

Turn in a PRINTOUT of your program (no sample data necessary) with your full name and netID, in class; AND, e-mail the source code to oneill+assign3@cs.cornell.edu.

Please create a separate .c file for each problem.

DO NOT just e-mail me your source code. YOU MUST turn in a printout to receive credit for the assignment.

Problem: Reversing a sentence

For this problem (which I discovered while looking for programming questions that are likely to be asked during interviews for programming positions), you must prompt the user for a string corresponding to a sentence, and print the words of the sentence in reverse. For example, if the input string is "My name is Caleb Nichol", the output must be "Nichol Caleb is name My". You should treat a "word" of the sentence as any contiguous block of characters not containing the space character ' '.

You may assume that the input line is of lenth <= 100 characters. For reading strings of text, you should use the function fgets.

It is strongly recommended that you write a function that takes a string and performs the entire manipulation:

void reverse_sentence( char *s );

And here's a big hint: you may find it quite useful to write a helper function:

void reverse_substring( char *s, int start, int end );

that reverses the block of characters in the string s starting at position start and ending at position end-1. For example, calling reverse_substring(s,0,n) for a string of length n would reverse all the characters in the string. (So if the string was "Lisa Bonet ate no basil" the resulting string would be "lisab on eta tenoB asiL".)

Here are some sample interactions with my program:

Please enter a sentence: My name is Caleb Nichol
Backwards: Nichol Caleb is name My


Please enter a sentence: What's your problem?
Backwards: problem? your What's


Please enter a sentence: Lisa Bonet ate no basil
Backwards: basil no ate Bonet Lisa
My solution uses two functions other than main, and has 23 lines of code inside functions.

Problem: Anagrams

Anagrams are pairs of phrases with the property that every letter of the alphabet appears the same number of times in each phrase.

For this problem you will write a program that asks the user for a file containing lines of text. If the file exists and is readable, the program will read (and print) two lines at a time and indicate whether the pair of phrases is an anagram.

For the purposes of determining whether a phrase pair is an anagram, your program should ignore all non-alphabetic characters and should be case insensitive. Also, you may assume that all input lines are of length <= 100 characters.

If you choose, you may simplify the assignment by prompting the user to input pairs of lines rather than reading them from a file. (Your assignment will still be eligible to receive a grade of "check" if you choose this option.)

Suppose that the file anagrams.txt contains the following lines of ASCII text:

Seth Cohen
Summer Roberts
Seth Cohen
The Chosen
Marissa Cooper
I am a processor
Snooze alarms
Alas!  No more Z's
Snooze alarms
I'm lazy
Eleven plus two
Twelve plus one
Five plus two
Three plus four
Dirty room
Dormitory
The Florida Vote Recount
Done To Cover Their Fault
This line is not in a pair

Here is a sample interaction with my program:

Enter a filename containing pairs of phrases: anagrams.txt

'Seth Cohen' and 'Summer Roberts' are not anagrams.

'Seth Cohen' and 'The Chosen' are anagrams.

'Marissa Cooper' and 'I am a processor' are anagrams.

'Snooze alarms' and 'Alas!  No more Z's' are anagrams.

'Snooze alarms' and 'I'm lazy' are not anagrams.

'Eleven plus two' and 'Twelve plus one' are anagrams.

'Five plus two' and 'Three plus four' are not anagrams.

'Dirty room' and 'Dormitory' are anagrams.

'The Florida Vote Recount' and 'Done To Cover Their Fault' are anagrams.

There are no more phrase pairs to be read.
Note: my solution uses one extra function with 17 lines of code, and main() has 23 lines of code (mostly to take care of file-reading stuff).