Programming Fundamentals/Strings

From Wikiversity
Jump to navigation Jump to search

This lesson introduces strings and string processing.

Objectives and Skills[edit]

Objectives and skills for this lesson include:


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


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



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()


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()



buffer overflow
An anomaly where a program overruns a memory storage location and overwrites adjacent memory locations.[7]
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]
Takes elements of an array/list, joins them together, and makes them into a string. Can also use a separator between each element.[10]
Standard output error stream, typically the monitor.[11]
Standard input stream, typically the keyboard.[12]
Standard output stream, originally a printer, but now typically a monitor.[13]
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]
Remove leading and trailing spaces from a string.[16]

See Also[edit]


  1. "Standard streams" (in en). Wikipedia. 2019-03-05. 
  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.
  10. Wikipedia: Join (SQL)
  14. Wikipedia: String (computer science)