Longest Word
In this activity,
you will make a program
called longestWord.c
.
This program should read in characters
using getchar()
until the end of input is reached.
Your program should determine the longest word, from the input it scanned in.
For simplicity, characters that are part of a word are
- lowercase and uppercase letters,
- numbers,
- the apostrophe,
- the hyphen (
-
), and - underscore (
_
). Every other character is considered a separator between words. You are also allowed to assume that the longest word will be less than 255 characters long.
Remember, you may not always start or end with a word and multiple word separators doesn’t indicate multiple words.
For example:
$ ./longestWord The quick brown fox jumped over the lazy dog. Ctrl+D The longest word was: jumped
If there is a tie for the longest word, your program should print the first of the longest words, e.g.
$ ./longestWord Man, I can't, I shan't, formulate an anthem where the words comprise mnenonics dreaded mnenonics for pi. Ctrl+D The longest word was: formulate
Remember,
getchar()
will read in the next character from input
and return it.
When it reaches the end of input,
it will return the value EOF
.
You can tell a program
to stop reading characters
by using Ctrl+D
which will cause getchar()
to return the EOF
value.
To read from a file
instead of typing in text,
you can use <
.
For example:
$ ./longestWord < myTestFile.txt
To tell the program you have finished typing, you can press Ctrl+D.
To run some simple automated tests:
$ 1511 autotest longestWord
To run Styl-o-matic:
$ 1511 stylomatic longestWord.c Looks good!
You’ll get advice if you need to make changes to your code.
Submit your work with the give command, like so:
$ give cs1511 wk06_longestWord
Or, if you are working from home, upload the relevant file(s) to the wk06_longestWord activity on Give Online.