Programming Fundamentals/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
editObjectives and skills for this lesson include:
- Manipulate string data
Readings
editMultimedia
editExamples
editActivities
editComplete 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.
- 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 asLastname, F.
Include a trailing period after the first initial. Handle invalid input errors, such as extra spaces or missing name parts. - 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. - 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.
- 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- 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
editFunction | 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()
|
Terms
edit- 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
editSee Also
editReferences
edit- ↑ Braunschweig, Dave. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/string-functions/.
- ↑ Braunschweig, Dave. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/strings/.
- ↑ Braunschweig, Dave. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/string-functions/.
- ↑ Braunschweig, Dave. Programming Fundamentals (in en). https://press.rebus.community/programmingfundamentals/chapter/string-functions/.
- ↑ https://press.rebus.community/programmingfundamentals/chapter/strings/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/string-formatting/
- ↑ "String Concatenation and Formatting". Python For Beginners. Retrieved 2019-11-08.
- ↑ https://press.rebus.community/programmingfundamentals/chapter/loading-an-array-from-a-text-file/
- ↑ Wikipedia: Join (SQL)
- ↑ https://www.w3schools.com/python/ref_string_split.asp
- ↑ https://stackoverflow.com/questions/8385322/difference-between-static-memory-allocation-and-dynamic-memory-allocation
- ↑ Wikipedia: String (computer science)
- ↑ https://press.rebus.community/programmingfundamentals/chapter/string-formatting/
- ↑ https://press.rebus.community/programmingfundamentals/chapter/string-formatting/