Lab 10: C-Strings

Feb 8, 2024  β”‚  m. Feb 7, 2024 by Charlotte Curtis

Objectives

Setup

Before starting on this lab, double check your git configuration. Run the following:

$ git status

You should see something like:

On branch main
Your branch is ahead of 'teacher/main' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

where the specific number of commits may vary.

If this is the case, you’re good to do the usual git pull to fetch the new lab starter code.

If instead you see something like:

On branch main
Your branch and 'teacher/main' have diverged,
and have 1 and 2 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

you may need to run git pull --no-rebase instead of the usual git pull to make sure you don’t end up in another dangling commit situation.

Finally, cd into the cstrings directory to begin the lab.

Warm-up Exercises

  1. Declare and initialize a C-string variable so that it contains your first name.

  2. On paper, draw a picture of memory (e.g. boxes filled with characters) for each of the following declarations:

    char s1[9] = "C-string";
    char s2[5] = "hi";
    char s3[] = " mom!";
    char s4[] = "";
    

    Consider using Python Tutor to check your answers

  3. Design and implement a function find_char_str which takes a C-string, a character and a position. The function should return the index position of the first occurrence of the character in the C-string at or after the given position. If the character is not found a -1 should be returned.

    You’ll also need to write a main function to test your function

Main Exercises

  1. Write a C++ function that counts the number of words in a sentence (a C-string, naturally) and returns the count. Call the function from main and print out the result as follows:

    There are # words in this sentence.
    OR
    There is 1 word in this sentence.
    

    Where # is the actual number of words in the sentence.

    Assumptions:

    • there is no punctuation in the sentence,
    • there is exactly one space between words,
    • the sentence has no leading or trailing whitespace

    Note: this is the tested function, but it’s still a good idea to test it yourself from main

  1. Write a C++ program (using functions as appropriate) that:

    • reads a C-string that will hold a person’s full name
    • using this full name C-string, create a second C-string that is this person’s initials, with periods, and last name

    For example:

    • “John Doe” would become: “J. Doe”
    • “Harry James Potter” would become “H.J. Potter”

    Assume that the longest possible name is 64 characters and that there is exactly one space between names.

Assessment

To build and run the test for this lab, cd to your top-level labs directory and run the command:

$ make lab=cstrings

When you are satisfied with your num_words function, add and commit your changes to your labs repo, then git push to submit. Your instructor will be building and running the exact same tests to determine whether you passed the lab.



Previous: Lab 09: Arrays
Next: Lab 11: Structures