Programming Fundamentals/Practice: Strings and Files

Chapter Summary

 * Strings - An array of single digits or letters that is typically used to display to the user or is the user's input.
 * Arrays - Sequenced collections of elements of the same data type with a single identifier name.
 * String Functions - String functions are used in computer programming languages to manipulate a string or query information about a string.
 * String Formatting - String formatting uses a process of string interpolation (variable substitution) to evaluate a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.
 * File Input and Output -A computer file is a computer resource for recording data discretely in a computer storage device. By using computer programs, a person can open, read, change, and close a computer file. Computer files may be reopened, modified, and copied an arbitrary number of times.
 * Loading an Array from a Text File - Loading an array from a text file requires several steps, including: opening the file, reading the records, parsing (splitting) the records into fields, adding the fields to an array, and closing the file.
 * Error Handling - Anticipating conditions that may cause errors when a program runs.
 * Dynamic/Static Memory - Memory associated with local and global scope respectively.

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) A filespec refers to a very small (like a spec dust) file.
 * 6) 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.
 * 7) String length and storage are handled by the compiler or interpreter, not the user or programmer.
 * 8) A string can be both a literal constant and a variable.
 * 9) An array with string values can be edited in all the same ways as a single string.
 * 10) A program can only read a file line by line and process one record at a time.

Answers:


 * 1) false
 * 2) true
 * 3) false - Typedef is used to assign alternative names to any existing datatypes, which is mostly used with user-defined datatypes.
 * 4) false
 * 5) false
 * 6) true
 * 7) true
 * 8) true - A literal constant is a value, which can be a string.
 * 9) true
 * 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.
 * 4) Describe (in your chosen language) how you would:
 * 5) * Change text to all uppercase
 * 6) * Get rid of the letter "b" from the given string.
 * 7) * string = "Hey buddy!"
 * 8) Describe the different ways you can read the contents of a file.
 * 9) Describe how string manipulation differs from variable manipulation

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  . Use string functions/methods to parse the line and print out the name in the form last name, first initial, such as   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, trailing, and duplicate spaces, and then print the line of text backwards. For example:
 * 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:

File Activities
Note: Each of the following activities uses code only to read the file. It is not necessary to use code to create the file.
 * 1) Using a text editor or IDE, copy the following list of names and grade scores and save it as a text file named  :        Create a program that displays high, low, and average scores based on input from  . 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. Format the average to two decimal places. Note that the program must work for any given number of scores in the file. Do not assume there will always be six scores.
 * 2) Create a program that displays high, low, and average scores based on input from  . 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. Format the average to two decimal places. 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 always be six scores.
 * 3) Create a program that asks the user for the name of a text/HTML file that contains HTML tags, such as:  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:
 * 4) Using a text editor or IDE, create a text file of names and addresses to use for testing based on the following format:    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: