# 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

Objectives and skills for this lesson include:
* Manipulate string data

## Readings

* Pressbooks: Programming Fundamentals
* Wikipedia: String (computer science)

## Multimedia

* YouTube: Programming Basics #28 String Concatenation
* YouTube: What Are Strings In Programming?
* YouTube: Computer programming tutorial: How to use strings
* Youtube: Strings - Intro to Computer Science
* YouTube: Programming/Scripting Concepts Explained (Variables, Arrays, Strings, & Length)

## Examples

* Flowchart
* Pseudocode
* BASIC
* C
* C++
* C#
* Clojure
* COBOL
* Java
* JavaScript
* Lua
* Perl
* PHP
* PowerShell
* Python3
* Ruby
* Swift
* VB.NET

## Activities

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.
* 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.
* 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

* 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.
* 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.

### String Functions

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

## Terms
### buffer overflow

    An anomaly where a program overruns a memory storage location and overwrites adjacent memory locations.

### code injection

    The exploitation of a computer bug that is caused by processing invalid data.

### concatenation

    The process in which you add two strings together in order to create a longer string

### dynamic memory

    Stack created memory associated with local scope. You now control the exact size and the lifetime of these memory locations.

### join

    Takes elements of an array/list, joins them together, and makes them into a string. Can also use a separator between each element.

### split

    Splits a string into a list.

### 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.

### string

    In most programming languages a string is a sequence of characters, either as a literal constant or as some kind of variable.

### 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.

### string interpolation

    Evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

### 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

* Quizlet: Flash Cards
* Quizlet 2

## See Also

* Wikipedia: Comparison of programming languages (string functions)