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 single 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, if shifting the line to the left:
        Repeat this. Repeat this. 
        epeat this. Repeat this. R
        peat this. Repeat this. Re
    Or if shifting the line to the right:
        Repeat this. Repeat this. 
         Repeat this. Repeat this.
        . Repeat this. Repeat this

Lesson Summary[edit]

  • In computer programming, standard streams are pre-connected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin – keyboard), standard output (stdout – originally a printer) and standard error (stderr – monitor). Streams may be redirected to other devices and/or files. In current environments, stdout is usually redirected to the monitor.[1]
  • It is considered good programming practice to determine if the file was opened properly. The reason the operating system usually can’t open a file is because the filespec is wrong (misspelled or not typed case consistent in some operating systems) or the file is not stored in the location specified. Accessing files stored on a network or the Internet may fail due to a network error.[2]
  • Most current programming languages include built-in or library functions to process strings. Common examples include case conversion, comparison, concatenation, find, join, length, reverse, split, substring, and trim.[3]
  • 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. The file may be read all at once and then parsed, or processed line by line. The array must either be at least as large as the number of records in the file, or must be generated dynamically.[4]

String Functions[edit]

Function C++ C# Java
case tolower(), toupper(), etc. ToLower(), ToUpper(), etc. toLowerCase(), toUpperCase(), etc.
comparison <, >, ==, etc. <, >, ==, etc. <, >, ==, etc.
concatenation +, += +, += +, +=
find find() IndexOf() indexOf()
join N/A Join() join()
length length() Length length()
replace replace() Replace() replace()
reverse reverse() Reverse() N/A
split strtok() Split() split()
substring substr() Substring() substring()
trim N/A Trim() trim()

[5]

Function JavaScript Python Swift
case toLowerCase(), toUpperCase(), etc. lower(), upper(), etc. lowercased(), uppercased()
comparison <, >, ==, etc. <, >, ==, etc. <, >, ==, etc.
concatenation +, += +, += +, +=
find indexOf() find() firstIndex()
join join() join() joined()
length length len() count
replace replace() replace() replacingOccurrences()
reverse N/A string[::-1] reversed()
split split() split() split()
substring substring() string[start:end] string[start...end]
trim trim() strip()

[6]

Terms[edit]

buffer overflow
An anomaly where a program overruns a memory storage location and overwrites adjacent memory locations.[7]
concatenation
Combining two strings into one string.[8]
device token
A key value provided by the operating system to associate a device to your program.[9]
join
Takes elements of an array/list, joins them together, and makes them into a string. Can also use a separator between each element.[10]
stderr
Standard output error stream, typically the monitor.[11]
stdin
Standard input stream, typically the keyboard.[12]
stdout
Standard output stream, originally a printer, but now typically a monitor.[13]
string
A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable.[14]
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.[15]
trim
Remove leading and trailing spaces from a string.[16]

See Also[edit]

References[edit]

  1. "Standard streams" (in en). Wikipedia. 2019-03-05. https://en.wikipedia.org/w/index.php?title=Standard_streams&oldid=886304371. 
  2. Busbee, Kenneth Leroy. Programming Fundamentals.
  3. Braunschweig, Dave. Programming Fundamentals.
  4. Braunschweig, Dave; Busbee, Kenneth Leroy. Programming Fundamentals.
  5. Braunschweig, Dave. Programming Fundamentals.
  6. Braunschweig, Dave. Programming Fundamentals.
  7. https://press.rebus.community/programmingfundamentals/chapter/strings/
  8. https://press.rebus.community/programmingfundamentals/chapter/strings/
  9. https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/
  10. Wikipedia: Join (SQL)
  11. https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/
  12. https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/
  13. https://press.rebus.community/programmingfundamentals/chapter/file-input-and-output/
  14. Wikipedia: String (computer science)
  15. https://press.rebus.community/programmingfundamentals/chapter/string-formatting/
  16. https://press.rebus.community/programmingfundamentals/chapter/string-functions/