Lab 5: Fun with Strings

Due: Wednesday, July 28, 2021 (11:59 PM PDT)

Introduction

The assignment for this week will utilize concepts of string manipulations and sorting algorithms.

We will also be grading for meeting requirements, using “class legal” code, and plagiarism. So, it is not enough for your lab to just pass the Gradescope autograder! Please read the instructions herein carefully.

It is highly recommended that you develop the algorithms for each program first and then develop the C++ code for it.

Step 1: Getting ready

Either through a graphical file system explorer or through the terminal:

  1. Navigate to your cs16 directory:

    $ cd cs16
  2. Create and navigate to the lab5 directory:

    $ mkdir lab5
    $ cd lab5
  3. Download the starter files to your lab5 directory. Either:

    1. Download these starter files from the course website next to the Lab 5 instructions link: http://sites.cs.ucsb.edu/~zsisco/cs16/#labs

    2. Or, if you are using CSIL, run the following commands from your lab5 directory:

    $ cp ~zsisco/public_html/cs16/lab5-files.zip . 
    
    $ unzip lab5-files.zip

Step 2: Create and edit your C++ programs

This week, you will need to create two (2) C++ programs called sentence.cpp and anagrams.cpp. They are worth 50 points each and have to be submitted properly for full assignment credit.

Important Note: We will take major points off if you use C++ instructions/libraries/code that either (a) was not covered in class, or (b) was found to be copied from outside sources (or each other) without proper citation.

sentence.cpp

The program will ask the user for a sentence and will then, re-arrange the letters in the sentence by

The program will then show the frequency of every letter in the string (but just the alphabetical characters). You have to distinguish between upper and lower-case alphabet characters.

For example, see the 2 runs below:

$ ./sentence
Enter sentence: How now brown cow?
Sorted and cleaned-up sentence:Hbcnnoooorwwww
H: 1
b: 1
c: 1
n: 2
o: 4
r: 1
w: 4
$ ./sentence
Enter sentence: $1,234.00
Sorted and cleaned-up sentence:

Requirements

  1. Your program must utilize the Bubble Sort algorithm (not another sorting algorithm) that we reviewed in lecture (and also found in Ch. 7 of your textbook). You will need to adapt the algorithm to a string, rather than to an array of integers.
  2. Be sure to utilize only techniques we’ve covered in lecture. Do not use “special” arrays, do not use vectors, do not use built-in sorting functions, etc. You will get zero points if you do.
  3. Start your program using the sentence.cpp program that I’ve provided you with. The skeleton program shows 2 called functions, which, of course, you must use. You can create more functions in your program if you need to (you likely will).

Hint: - Realize that, in C++, assigning char types their ASCII code (which are integers) is legal. For example char letter = 65; assigns A to letter. See https://simple.wikipedia.org/wiki/ASCII for a list of ASCII character codes.

anagrams.cpp

This is a program that determines if 2 strings are anagrams. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, for example, the letters in the word “listen” can be re-arranged to spell “silent”.

The function should not be case sensitive and should disregard any numerals, punctuation or spaces (i.e. any non-alphabet characters). Two strings are anagrams if the letters can be rearranged to form each other. For example, “Eleven plus two” is an anagram of “Twelve plus one”. Each string contains one “v”, three “e’s”, two “l’s”, etc. You may use C++ strings and arrays to solve this problem.

Write a program that inputs two strings and calls your function to determine whether or not the strings are anagrams and prints the result. A session should look like one of the following examples (including whitespace and formatting).

$ ./anagrams
Enter first string:
Eleven plus two
Enter second string:
Twelve plus three
The strings are not anagrams.
$ ./anagrams
Enter first string:
Rats and Mice
Enter second string:
in cat's dream
The strings are anagrams.

Requirements

  1. Be sure to utilize only techniques we’ve covered in lecture. Do not use “special” arrays or pointers, do not use vectors, do not use built-in sorting functions, etc. You will get zero points if you do.
  2. Start your program using the anagrams.cpp program that I’ve provided you with. The skeleton program shows called functions, which, of course, you must use. You can create more functions in your program if you need to.

Hint: - You can create “counting arrays”, that are integer arrays that keep track of how many of what letters are found in each input string. For example, array1 has 26 elements, each representing a letter in one of the strings, etc. This is not the only approach that you can use, of course.

Step 3: Create a makefile and compile the code

In order to learn another way to manage our source codes and their compilations, we will first create a makefile and put in the usual g++ commands in it. Afterwards, whenever we want to compile our programs, the command is a lot shorter—so this is a convenience.

Using your text editor, create a new file called makefile and enter the following into it:

all: sentence anagrams

sentence: sentence.cpp
    g++ -o sentence sentence.cpp -Wall -std=c++11

anagrams: anagrams.cpp
    g++ -o anagrams anagrams.cpp -Wall -std=c++11

Then from the command line, you can issue one command that will compile this code, like so:

$ make

If the compilation is successful, you will not see any output from the compiler. You can then run your program like this:

$ ./sentence

or

$ ./anagrams

If you encounter an error, use the compiler hints and examine the line in question. If the compiler message is not sufficient to identify the error, you can search online to see when the error occurs in general.

Remember to re-compile the relevant files after you make any changes to the C++ code.

Step 4: Submit your programs for grading

Once you are satisfied your programs are correct, then it’s time to submit them. While working with others is OK, you still must submit your own lab. Even if you’re working with another person, do not copy each other’s code.

Log into Gradescope and select CMPSC 16 under Summer 2021, and navigate to the Lab 5 assignment. Then click on the “Upload Submission” button on the bottom right corner to make a submission.

You will be given the option of uploading files from your local machine or submitting code from a GitHub repo. Follow the steps to upload both sentence.cpp and anagrams.cpp to Gradescope (do not upload any other files). If your files do not have those exact names as presented in this lab, the autograder will not grade them. You can resubmit your files as many times as you like before the assignment deadline.