Jump to content

Programming Fundamentals/Strings

From Wikiversity
Strings
Strings

This lesson introduces strings and string processing. A string is a data type used to represent integers instead of numbers in programming. This data type can contain spaces and numbers as well. You can edit string data types by something called string processing. This is when you edit the data in the string by adding, moving, or replacing characters using specific string functions.

Objectives and Skills

[edit | edit source]

Objectives and skills for this lesson include:

  • Manipulate string data

Readings

[edit | edit source]
  1. Pressbooks: Programming Fundamentals
  2. Wikipedia: String (computer science)

Multimedia

[edit | edit source]
  1. YouTube: Programming Basics #28 String Concatenation
  2. YouTube: What Are Strings In Programming?
  3. YouTube: Computer programming tutorial: How to use strings
  4. Youtube: Strings - Intro to Computer Science
  5. YouTube: Programming/Scripting Concepts Explained (Variables, Arrays, Strings, & Length)

Examples

[edit | edit source]

Activities

[edit | edit source]

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, trailing, and duplicate 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 | edit source]
  • 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.[1]
  • Most current programming languages implement strings as a data type or class where strings are stored as a length controlled array. String length and storage are handled by the compiler or interpreter, reducing program errors.[2]

String Functions

[edit | edit source]
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()

[3]

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

[4]

Terms

[edit | edit source]
buffer overflow
An anomaly where a program overruns a memory storage location and overwrites adjacent memory locations.[5]
code injection
The exploitation of a computer bug that is caused by processing invalid data.[6]
concatenation
The process in which you add two strings together in order to create a longer string[7]
dynamic memory
Stack created memory associated with local scope. You now control the exact size and the lifetime of these memory locations.[8]
join
Takes elements of an array/list, joins them together, and makes them into a string. Can also use a separator between each element.[9]
split
Splits a string into a list.[10]
static memory
Memory for your variables is allocated when the program starts. The size is fixed when the program is created. It applies to global variables, file scope variables, and variables qualified with static defined inside functions.[11]
string
In most programming languages a string is a sequence of characters, either as a literal constant or as some kind of variable.[12]
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.[13]
string interpolation
Evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.[14]
title
A string method which is used to convert the first character in each word to uppercase and the remaining characters to lowercase and returns new string.

Assessments

[edit | edit source]

See Also

[edit | edit source]

References

[edit | edit source]
  1. Braunschweig, Dave. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/string-functions/. 
  2. Braunschweig, Dave. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/strings/. 
  3. Braunschweig, Dave. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/string-functions/. 
  4. Braunschweig, Dave. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/string-functions/. 
  5. https://press.rebus.community/programmingfundamentals/chapter/strings/
  6. https://press.rebus.community/programmingfundamentals/chapter/string-formatting/
  7. "String Concatenation and Formatting". Python For Beginners. Retrieved 2019-11-08.
  8. https://press.rebus.community/programmingfundamentals/chapter/loading-an-array-from-a-text-file/
  9. Wikipedia: Join (SQL)
  10. https://www.w3schools.com/python/ref_string_split.asp
  11. https://stackoverflow.com/questions/8385322/difference-between-static-memory-allocation-and-dynamic-memory-allocation
  12. Wikipedia: String (computer science)
  13. https://press.rebus.community/programmingfundamentals/chapter/string-formatting/
  14. https://press.rebus.community/programmingfundamentals/chapter/string-formatting/