Programming Fundamentals/Strings

From Wikiversity
Jump to navigation Jump to search
Strings

This lesson introduces strings and string processing.

Objectives and Skills[edit]

Objectives and skills for this lesson include:

Readings[edit]

  1. Rebus: Programming Fundamentals
  2. Wikipedia: String (computer science)

Multimedia[edit]

  1. YouTube: Programming Basics #12 Characters and Strings
  2. YouTube: Programming Basics #28 String Concatenation
  3. YouTube: Programming Basics #29 Patterns in Strings

Examples[edit]

Activities[edit]

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.

  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
    Use separate subroutines/functions/methods to implement input, each type of processing, and output. Avoid global variables by passing parameters and returning results.
  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

See Also[edit]

References[edit]