Chapter 12: Text Processing Starting Out with Programming Logic & Design Second Edition by Tony Gaddis
Chapter Topics 12.1 Introduction 12.2 Character-by-Character Text Processing
12.1 Introduction Table 12-1 (see page 460) lists the common string functions covered so far. These functions operate on strings. Sometimes, however, you need to operate on the individual characters in a string.
12.2 Character-by-Character Text Processing Most languages provide a way to access the individual characters in a string. Often this is done with subscript notation, similar to accessing the elements of an array. Pseudocode example: Declare String greeting = "Hello world" Display greeting[0] Displays the first character in the string.
12.2 Character-by-Character Text Processing Subscript 0 accesses the first character, subscript 1 accesses the second character, and so on…
12.2 Character-by-Character Text Processing A loop can be used to step through all of the characters in a string.
12.2 Character-by-Character Text Processing This technique also allows characters to modified: Declare String str = "Coffee" str[0] = "T" Display str Changes the first character to "T" Displays "Toffee"
12.2 Character-by-Character Text Processing Table 12-2 Common Character Testing Functions Function Description isDigit(character) Returns True if character is a numeric digit, or False otherwise. isLetter(character) Returns True if character is an alphabetic letter or False otherwise. isLower(character) Returns True if character is a lowercase letter or False otherwise. isUpper(character) Returns True if character is an uppercase letter or False otherwise. isWhiteSpace (character) Returns True if character is a whitespace character or False otherwise. (A whitespace character is a space, a tab, or a newline.)
12.2 Character-by-Character Text Processing Example from Program 12-4 (see page 464) to count the number of uppercase characters in a string. str is a String variable that contains a valid string.
12.2 Character-by-Character Text Processing Table 12-3 String Insertion and Deletion Modules Function Description insert(string1, position, string2) string1 is a String, position is an Integer, and string2 is a String. The function inserts string2 into string1, beginning at position. delete(string, start, end) string is a String, start is an Integer, and end is an Integer. The function deletes from string all of the characters beginning at the position specified by start, and ending at the position specified by end. The character at the ending position is included in the deletion.
12.2 Character-by-Character Text Processing Example: Declare String str = "New City" insert(str, 4, "York ") Display str Displays "New York City"
12.2 Character-by-Character Text Processing Example: Declare String str = "I ate 1000 blueberries!" delete(str, 8, 9) Display str Displays "I ate 10 blueberries!"