Looping and Branching
Strings
The string data type covers blocks of text/characters. They can either be multiple character or single character strings. Strings can be compared. For one string to be equal to another string, it must be exactly the same. Strings are compared alphabetically throughout their lengths. Strings are also case sensitive.
"banner" = "banner" - these are exactly the same. "banner" < "banter" - 'n' < 't' in character order. "BANNER" ≠ "banner" - these are not the same, different case. "Banner" < "banner" - 'B' < 'b' in character order. "banner" < "tanner" - 'b' < 't' in character order. " "banner < "banner" - ' ' < 'b' in character order.
Strings are indexed. For the string "banner", character 'b' occupies position 1. The position is the string index. They are referenced in C++ as below.
banner[1] - would return the value 'b'
Spaces in a string are also indexed. In the example below the spaces are indexed at position 3 and 9.
My first string
Strings must not be referenced outside their range. The string above has a range of 1 to 15. Attempting to reference this for String[17] should produce an exception generated by the OS.
Strings have length. The string above has a length of 15. An empty/null string is denoted "" and has a length of 0, it has no characters. However a string of " " has a spacing character and has a length of 1. The length of a string can be referenced by the Length function to give an integer of the strings length. It does not compare strings as it is applied to a single string.
C++ declares string variables through the use of AnsiString (not a keyword).
Problems with Strings...
A program is required whose purpose is to receive a line of English texy entered from the keyboard, and to write out the number of words.
Words are separated by spaces. What about spaces? A user may enter space(s) before or after the line of text. Similarly a user may enter more than one space where we may expect only one. How do we account for such user errors?
Top level design
- 1. read in line of text
- 2. count words in line of text
- 3. write out count of words
Preliminary Variable Table
| Type | Identifier | Description |
| String | Line | Line of text input by user |
| Integer | WordCount | A count of the words in the input text |
Refining the first step of the design
- 1.1 write out "Enter a line of text with only a single space between each word. It may not start or end with a space. It must not be an empty line."
- 1.2 read in Line
- 2. count words in Line
- 3. write out "Number of words in text is", WordCount
Revised Variable Table
| Type | Identifier | Description |
| String | Line | Line of text input by user |
| Integer | WordCount | A count of the words in the input text |
| Integer | Index | For characters in the line |
| Integer | SpaceCount | A count of the spaces in the input text |
Refining the second step of the design
- 1.1 write out "Enter a line of text with only a single space between each word. It may not start or end with a space. It must not be an empty line."
- 1.2 read in Line
- 2.1 initialise any variables as necessary
- 2.2 loop while there are more characters in the line
- 2.3 process current character
- 2.4 loopend
- 3. write out "Number of words in text is", WordCount
We must initialise the variable Index to the value 1. The variable Index is incremented by 1 on each iteration until each character in Line has been scanned. The control for this is via the function Length (Line). i.e. loop while Index <= Length (Line).
SpaceCount must be initialised to 0.
Further refining of the second step of the design produces a final design of the simple problem.
- 1.1 write out "Enter a line of text with only a single space between each word. It may not start or end with a space. It must not be an empty line."
- 1.2 read in Line
- 2.1.1 Index <— 1
- 2.1.2 SpaceCount <— 0
- 2.2 loop while Index <= Length (Line)
- 2.3.1 if Line [Index] = ' ' then
- 2.3.2 SpaceCount <— SpaceCount + 1
- 2.3.3 ifend
- 2.3.4 Index <— Index + 1
- 2.4 loopend
- 2.5 WordCount <— SpaceCount + 1
- 3.1 write out "Number of words in text is", WordCount
This design can be coded and expected to run. What about the spaces? We should expect that the user would do what is asked. But, what if?
But, what about spaces? As has been said, a user may enter space(s) before or after the line of text. Similarly a user may enter more than one space where we may expect only one. How do we account for such user errors? How can we improve this?
Comments, suggestions, ideas to
Stuart Banner
