This lab will give you more practice with the
String
class
we
developed in lecture (as well as with operator overloading).
Develop a substring operator using operator()
.
The arguments should be the starting position for the substring
and the length of the substring. The result should be a
copy of the substring starting at the given position within our
own data
and ending at most the given length from the start. For example:
Object's Value Operator Call Resulting Object's Value "bob wuz here" obj(0,4) "bob " "bob wuz here" obj(8,14) "here" (ran out of characters) "bob wuz here" obj(14,4) "" (nothing there) "bob wuz here" obj(4,1) "w"
If the start position doesn't exist, send back an empty string (as we've defined). If there aren't 'length' characters left in our string after the given start position, return what we do have.
Don't forget to write a test application to show that your operator works. (Perhaps you could modify the existing String test app?)
String
s? (Does your
operator change the original String object?)
This assignment is (Level 2).
Add (Level 1.5) to allow for negative lengths. I'll leave it up to you to decide if the resulting String should be forwards:
Object's Value Operator Call Resulting Object's Value "bob wuz here" obj(0,-4) "" (nothing there) "bob wuz here" obj(8,-14) "bob wuz h" (ran out of characters) "bob wuz here" obj(14,-4) "e" "bob wuz here" obj(4,-1) "w" "bob wuz here" obj(4,-2) " w"
Or backwards:
Object's Value Operator Call Resulting Object's Value "bob wuz here" obj(0,-4) "" (nothing there) "bob wuz here" obj(8,-14) "h zuw bob" (ran out of characters) "bob wuz here" obj(14,-4) "e" "bob wuz here" obj(4,-1) "w" "bob wuz here" obj(4,-2) "w "
Add (Level 1) to overload the ~ operator to return the reversed elements of a String:
Object's Value Operator Call Resulting Object's Value "bob" ~obj "bob" "wuz" ~obj "zuw"
Make sure to test this operator, too.