Skip to main content
Engineering LibreTexts

7.12: Practice- Strings and Files

  • Page ID
    10699
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Review Questions

    True / False

    1. The character data type in C++ uses the double quote marks, like: char grade = “A”;
    2. Sizeof is an operator that tells you how many bytes a data type occupies in storage.
    3. Typedef helps people who can’t hear and is one of the standard accommodation features of a programming language for people with a learning disability.
    4. The sequence operator should be used when defining variables in order to save space.
    5. Programming can be both enjoyable and frustrating.
    6. Text files are hard to create.
    7. A filespec refers to a very small (like a spec dust) file.
    8. A device token is a special non zero value the operating system gives your program and is associated with the file that you requested to be opened.
    9. Programmers should not worry about closing a file.
    10. Where you define an item, that is global or local scope, is rarely important.

    Answers:

    1. false
    2. true
    3. false
    4. false
    5. true
    6. false
    7. false
    8. true
    9. false
    10. false

    Short Answer

    1. Describe the normal operations allowed with the string data type.
    2. Describe why unary positive is worthless.
    3. Describe how unary negative works.

    Activities

    Complete the following activities using pseudocode, a flowcharting tool, or your selected programming language. Use separate functions for input, each type of processing, and output. Avoid global variables by passing parameters and returning results. Create test data to validate the accuracy of each program. Add comments at the top of the program and include references to any resources used.

    String Activities

    1. Create a program that asks the user for a line of text containing a first name and last name, such as Firstname Lastname. Use string functions/methods to parse the line and print out the name in the form last name, first initial, such as Lastname, F. Include a trailing period after the first initial. Handle invalid input errors, such as extra spaces or missing name parts.
    2. Create a program that asks the user for a line of text. Use string functions/methods to delete leading and trailing spaces, and then print the line of text backwards. For example:
      the cat in the hat
      tah eht ni tac eht
    3. Create a program that asks the user for a line of comma-separated-values. It could be a sequence of test scores, names, or any other values. Use string functions/methods to parse the line and print out each item on a separate line. Remove commas and any leading or trailing spaces from each item when printed.
    4. Create a program that asks the user for a line of text. Then ask the user for the number of characters to print in each line, the number of lines to be printed, and a scroll direction, right or left. Using the given line of text, duplicate the text as needed to fill the given number of characters per line. Then print the requested number of lines, shifting the entire line’s content by one character, left or right, each time the line is printed. The first or last character will be shifted / appended to the other end of the string. For example:
      Repeat this. Repeat this.
      epeat this. Repeat this. R
      peat this. Repeat this. Re

    File Activities

    1. Using a text editor or IDE, create a text file of names and grade scores to use for testing based on the following format:
      Larry Fine: 80
      Curly Howard: 70
      Moe Howard: 90
      Using the file above, create a program that displays high, low, and average scores based on input from the file. Verify that the file exists and then use string functions/methods to parse the file content and display the high, low, and average score. Note that the program must work for any given number of scores in the file. Do not assume there will only be three scores.
    2. Using the file above, create a program that displays high, low, and average scores based on input from the file. Verify that the file exists and then use string functions/methods to parse the file content and add each score to an array. Display the array contents and then calculate and display the high, low, and average score. Note that the program must work for any given number of scores in the file. Do not assume there will only be three scores.
    3. Using the file above, create a program that displays high, low, and average scores based on input from the file. Verify that the file exists and then use string functions/methods to parse the file content and add each score to an array. Display the array contents and then calculate and display the high, low, and average score. Include error handling in case the file is formatted incorrectly. Note that the program must work for any given number of scores in the file. Do not assume there will only be three scores.
    4. Create a program that asks the user for the name of a text/HTML file that contains HTML tags, such as:

      <strong>This is a bold paragraph.</strong></p>


      Verify that the file exists and then use string methods to search for and remove all HTML tags from the text, saving each removed tag in an array. Display the untagged text and then display the array of removed tags. For example:
      This is a bold paragraph.


      <strong>
      </strong>
      </p>
    5. Using a text editor or IDE, create a text file of names and addresses to use for testing based on the following format:
      Firstname Lastname
      123 Any Street
      City, State/Province/Region PostalCode
      Include a blank line between addresses, and include at least three addresses in the file. Create a program that verifies that the file exists, and then processes the file and displays each address as a single line of comma-separated values in the form:
      Lastname, Firstname, Address, City, State/Province/Region, PostalCode

    7.12: Practice- Strings and Files is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?