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.
||Returns the number of characters in a string|
||Concatenates two strings|
||Converts to upper case|
||Converts to lower case|
||Returns a part of a string|
||Returns true if string1 contains string2|
||Converts a string to an integer|
||Converts a string to a real number|
||Returns true if the string can be converted to an Integer|
||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
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.