lab

EECS 280 Lab 3: Strings and I/O

Lab Due Sunday, October 4, 2020, 8:00 pm

In this lab, you will review C-style strings, C++ string objects, and file input/output by implementing a simple spell checker. Additionally, your lab instructor will go over the compilation sequence for C++ and make, which will help you understand the Makefiles you have been using for projects. Finally, we’ve prepared an Exam Prep that reviews similar topics.

You may work alone or with a partner. Please see the syllabus for partnership rules.

Submit the code files below on the autograder. We encourage you to complete the lab Exam Prep, but it is not turned in for credit.

Files to submit

Completion Criteria/Checklist:

To pass this lab, you must finish tasks 1 and 2.

Lab Exercises

The Files

We have provided starter files for this lab. Use the following commands in a terminal at your working directory to download the files.

$ wget eecs280staff.github.io/lab/lab03/starter-files.tar.gz
$ tar -xvzf starter-files.tar.gz

Here’s a summary of this lab’s files. You will turn in the bolded ones.

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++11 lab03.cpp main.cpp -o lab03.exe
$ ./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.

Task 3 - Makefile Tutorial

We’ve put together a tutorial on make, where you will learn about Makefiles and how to use them to automate processes like compiling your programs. You will find it at:

https://eecs280staff.github.io/p1-stats/setup_make.html

Submit

Submit the required files to the autograder.