Lab 3
You cannot submit for this problem because the homework's deadline is due.
EECS 280 Lab 3: Strings and I/O
In this lab, you will review C-style strings, C++ string objects, and file input/output by implementing a simple spell checker.
Submit the code files below on the autograder.
Files to submit
lab03.cpp
lab03.h
main.cpp
words.txt
Completion Criteria/Checklist:
To pass this lab, you must do the following:
[ ] Implement
strcmp_eecs280
.[ ] Implement
get_user_word
,read_words_from_file
, andfind_word
.
Lab Exercises
The Files
Here's a summary of this lab's files.
File | Description |
---|---|
lab03.h |
Contains the get_user_word and find_word function declarations. |
lab03.cpp |
Contains the get_user_word and find_word function definitions. |
main.cpp |
Contains the main function that runs testing code. |
words.txt |
A list of all properly spelled words, one word per line. |
Testing Code
The main
function in main.cpp
contains some testing code we've written for you, which will print the results produced by your code.
The starter code should compile successfully without any modifications, so make sure you are able to compile and run it with the following commands. The code may be missing some pieces, contain some bugs, or crash when you run it, but you'll fix each throughout the course of the lab.
$ g++ -Wall -Werror -g -pedantic --std=c++17 lab03.cpp main.cpp -o lab03
$ ./lab03.exe
Introduction
The overall goal of this lab is to learn about string manipulation and file input/output in C++, and to do this we'll work through the implementation of a simple spell checker. In order to determine if a word is spelled correctly, we will search through a text file containing an exhaustive list of correctly spelled words and look for an exact match.
Note: Here and throughout the lab, we assume all words are written using lowercase characters. This is true for the given reference file and you should only input words in lowercase when running the program.
Task 1 - Checking if Two Words Match
Let us first consider how we could solve this problem by using C-strings to represent words. Recall that a C-string is simply an array of characters that terminates with a null character, '\0'
. We don't have to use an extra variable to keep track of a C-string's length because we can use the null character as a sentinel for iteration.
So it seems clear we can determine if two words are the same by iterating through the C-strings and comparing characters one by one. Let's go a little bit farther and actually replicate the strcmp
function from the <cstring>
library, which determines how two C-strings are ordered. Find the strcmp_eecs280
function in your lab03.cpp
file and write an implementation according to the RME.
Note: strcmp_eecs280
(and the strcmp
function in the standard library) do not specify which positive or negative number they return. Thus, you should always check strcmp(str1, str2) < 0
instead of strcmp(str1, str2) == -1
.
Task 2 - Checking Against the Words File
Now that we have a way to compare words, we need to get a word from the user and compare it to the words in the reference file words.txt
. We want to prompt the user to enter a word, but because we don't know how long the word is ahead of time (i.e. before we run the program), using C-style strings would be difficult (think about why). Instead, we'll work with string
objects from the C++ Standard Library that can hold words of arbitrary sizes.
Write the get_user_word
function according to its RME. The initial implementation just returns "quit"
because that will cause the program to terminate. Replace it with your implementation.
Once we have the user's word, we need to compare it against each word in the words.txt
reference file. First, write the read_words_from_file
function, which reads words from a file into a vector. Then complete find_word
, which determines if a given word is contained in a vector of valid words. You must use strcmp_eecs280
in your implementation of find_word
.
Note: You can call str.c_str()
to convert from the C++ string str
to a C-style string.
Submit
Submit the required files to the autograder.