Assignment 2: Learn to C

Due Wednesday, February 1st, before midnight

The goals for this assignment are:

  • Work with strings

  • Work with structs

  • Work with arrays

1. Update your repository

Do a fetch upstream to obtain the basecode for this assignment.

The first time you do a fetch, you need to set the URL of the upstream repository.

  • git remote add upstream git@github.com:brynmawr-cs223-s23/cs223-assignments.git

Using the command line

  • Open terminal and change your current directory to your assignment repository.

  • Run the command git fetch upstream

  • Run the command git merge upstream/master

Your repository should now contain a new folder named A02.

The fetch and merge commands update your repository with any changes from the original.

2. Exclaim!

Sometimes life can be very frustrating and we just need to shout. Write a program, exclaim.c that turns a word into an exclaimation. Your program should loop through each character. If the character is a lowercase letter, replace it with a randomly chosen character from the set {'@', '!', '#', '*'}.

Two examples of the running program are shown below. User input is shown in bold.

$ make exclaim
gcc -g -Wall -Wvla -Werror exclaim.c -o exclaim
$ ./exclaim
Enter a word: apple
OMG! *#!*!
$ ./exclaim
Enter a word: YOLO!
OMG! YOLO!
$ ./exclaim
Enter a word: Tada!
OMG! T*#!!

Requirements and Hints:

  • You can assume that the user will enter a string that fits into a 32 character buffer, e.g. store the user’s input with a variable defined as char buff[32];

  • Use the fact that ascii characters correspond to integers to check for a lowercase letter. ASCII Table

  • Use rand() to choose a random character.

3. Acronym

Write a program, acronym.c, that computes an acronym from a given phrase. Your program should create a string consisting of all capital letters from the given phrase.

$ make acroynm
gcc -g -Wall -Wvla -Werror acronym.c -o acronym
$ ./acronym
Enter a phrase: What Does The Fox Say?
Your acronym is WDTFS
$ ./acronym
Enter a phrase: The Truth Is Out There
Your acronym is TTIOT
$ ./acronym
Enter a phrase: You Only Live Once
Your acronym is YOLO

Requirements and Hints:

  • You do not need to handle the case where a capital letter occurs in the middle of a word.

  • Watch out that any string you create ends with a terminating null character, e.g. '\0'

  • You can assume that all input fits within 1024 characters.

To read in a string containing phrases, use the following format string with scanf: scanf("%[^\n]%*c", phrase); The %[^\n] will read until the newline is reached. The %*c reads the newline and ignores it.

4. Songs

Implement a program, songs.c, that fills an array with three or more songs and then allows the user to edit the data in the array.

$ make songs
gcc -g -Wall -Wvla -Werror songs.c -o songs
$ ./songs
Welcome to Steven Struct's Song List.

0) Shout                artist: Tears for Fears         duration: 4:11  danceability: 0.50
1) As it was            artist: Harry Styles            duration: 2:47  danceability: 0.70
2) Wish you were here   artist: Pink Floyd              duration: 5:34  danceability: 0.30

=======================

Enter a song id to edit [0,1,2]: 1
Which attribute do you wish to edit? [artist, title, duration, danceability]: duration
Enter a duration (minutes): 2
Enter a duration (seconds): 40

0) Shout                artist: Tears for Fears         duration: 4:11  danceability: 0.50
1) As it was            artist: Harry Styles            duration: 2:40  danceability: 0.70
2) Wish you were here   artist: Pink Floyd              duration: 5:34  danceability: 0.30

=======================
$ ./songs
Welcome to Steven Struct's Song List.

0) Shout                artist: Tears for Fears         duration: 4:11  danceability: 0.50
1) As it was            artist: Harry Styles            duration: 2:47  danceability: 0.70
2) Wish you were here   artist: Pink Floyd              duration: 5:34  danceability: 0.30

=======================

Enter a song id to edit [0,1,2]: 2
Which attribute do you wish to edit? [artist, title, duration, danceability]: artist
Enter a artist: The Who

0) Shout                artist: Tears for Fears         duration: 4:11  danceability: 0.50
1) As it was            artist: Harry Styles            duration: 2:47  danceability: 0.70
2) Wish you were here   artist: The Who                 duration: 5:34  danceability: 0.30

=======================

$ ./songs
Welcome to Steven Struct's Song List.

0) Shout                artist: Tears for Fears         duration: 4:11  danceability: 0.50
1) As it was            artist: Harry Styles            duration: 2:47  danceability: 0.70
2) Wish you were here   artist: Pink Floyd              duration: 5:34  danceability: 0.30

=======================

Enter a song id to edit [0,1,2]: 5
Invalid choice!

$ ./songs
Welcome to Steven Struct's Song List.

0) Shout                artist: Tears for Fears         duration: 4:11  danceability: 0.50
1) As it was            artist: Harry Styles            duration: 2:47  danceability: 0.70
2) Wish you were here   artist: Pink Floyd              duration: 5:34  danceability: 0.30

=======================

Enter a song id to edit [0,1,2]: 0
Which attribute do you wish to edit? [artist, title, duration, danceability]: danceability
Enter danceability: 0.9

0) Shout                artist: Tears for Fears         duration: 4:11  danceability: 0.90
1) As it was            artist: Harry Styles            duration: 2:47  danceability: 0.70
2) Wish you were here   artist: Pink Floyd              duration: 5:34  danceability: 0.30

=======================

Requirements and Hints:

  • Your program should define a struct song to hold the song information. Duration should be stored in integers, danceability should be a float, and artist and title should be strings.

  • Your program should check for a valid song id and print an error message id the user enters an invalid id.

  • Your program should initialize at least 3 songs and store them in an array.

  • Your program should loop through the array of songs to print them in a table. Implementing this feature in a function is a good idea.

  • Your program should support spaces in the song names and titles.

  • Use strcmp to test which attribute the user wants to update.

  • Use strcpy to update the artist or title in the array.

  • Your program should print the songs in an attractive table, although the formatting does not need to match the sample output exactly.

  • You can assume that all song artists and titles fit into a 32 character buffer.

  • Use %-20s to format left-justified strings with 20 spaces of padding.

Because the song artists and titles can have spaces in them, using scanf requires additional syntax. Instead of scanf(" %d", &val);, use scanf(" %d%*c", &val);. The trailing %*c tells scanf to read and ignore the newline character at the end of the line. Furthermore, to read the name, call scanf(" %[^\n]%*c", artist); instead of scanf(" %s", artist);.

5. Submit your Work

Push you work to Github to submit your work.

$ cd A02
$ git add *.c
$ git commit -m "assignment 2 complete"
$ git push

6. Grading Rubric

Assignment rubrics

Grades are out of 4 points.

  • (1 point) exclaim

    • (0.1 points) style and header comment

    • (0.4 points) correct behavior: asks the user for input and creates the new string

    • (0.5 points) no memory errors

  • (1 point) acronym

    • (0.1 points) style and header comment

    • (0.4 points) correct behavior: asks the user for input and creates the new string

    • (0.5 points) no memory errors

  • (2 points) songs

    • (0.2 points) style and header comment

    • (0.8 points) correct behavior: reads file, prints output with columns and supports editing

    • (1.0 points) no memory errors

Code rubrics

For full credit, your C programs must be feature-complete, robust (e.g. run without memory errors or crashing) and have good style.

  • Some credit lost for missing features or bugs, depending on severity of error

  • -12.5% for style errors. See the class coding style here.

  • -50% for memory errors

  • -100% for failure to checkin work to Github

  • -100% for failure to compile on linux using make