Intro to Text

Processing

There are a number of programs that work extensively with text; word processors, text messaging programs, email programs, Web browsers, and spell checkers. Earlier in the course, we've learned about a dozen library functions that operate on strings.

Here is a list of common string library functions we've already discussed.

Function Description
length(string) Returns the number of characters in a string
append(string1, string2) Concatenates two strings
toUpper(string) Converts to upper case
toLower(string) Converts to lower case
substring(string, start, end) Returns a part of a string
contains(string1, string2) Returns true if string1 contains string2
stringToInteger(string) Converts a string to an integer
stringToReal(string) Converts a string to a real number
isInteger(string) Returns true if the string can be converted to an Integer
isReal(string) Returns true if the string can be converted to a Real.

We can display the first character in a string variable by using the subscript, starting with zero. For example, to display the first character in the String variable str, we can use the following statement

Display str[0]

We can use the len(string) to determine how many characters a string contains. Should we decide to display the last character in a string, we could use len(string) - 1 to get the position number of the last character. Since the subscripts start with zero, we must deduct one from the length of the string to get the exact position of the last character.

Therefore, to display the last character in a string named str we can use the pseudocode

Display str[length(str) - 1]

Sometimes we need to work with strings on a more detailed level - to access or manipulate individual characters in a string. For example, many Websites do not allow simple passwords any longer. They'll require uppercase letters, lower case letters, numbers, and special characters. These password requirements make the passwords more secure. We must write code to enforce the use of these new rules.

In the next section, we will take a look at character-by-character text processing.