Monday, June 18, 2012

Homework 3: StringObjects-2

1.  Write an expression that concatenates the string variable  suffix onto the end of the string variable  prefix .


prefix + suffix

2. Given the string variable  address , write an expression that returns the position of the first occurrence of the string "Avenue" in  address .
 
address . find ( "Avenue" )

3. Given a string variable  address , write a string expression consisting of the string "http://" concatenated with the variable's string value. So, if the value of the variable were "www.turingscraft.com", the value of the expression would be "http://www.turingscraft.com".

"http://" + address

4.  Given a string variable  word , write a string expression that parenthesizes the value of  word . So, if  word contains "sadly", the value of the expression would be the string "(sadly)"

"(" + word + ")"

5. Assume that  name is a variable of type  string that has been assigned a value. Write an expression whose value is a  string containing the last character of the value of  name . So if the value of  name were "Smith" the expression's value would be "h".

name. substr ( name. length ( ) - 1 )

6.  Assume that  sentence is a variable of type  string that has been assigned a value. Assume furthermore that this value is a string consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." 

 Assume that there is another variable declared,  firstWord , also of type  string . Write the statements needed so that the first word of the value of  sentence is assigned to  firstWord . So, if the value of  sentence were "Broccoli is delicious." your code would assign the value "Broccoli" to  firstWord . 

firstWord = sentence. substr ( 0 , sentence. find ( "`" ) ) ;

7. Assume that  sentence is a variable of type  string that has been assigned a value. Assume furthermore that this value is a string consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." 

 Assume that there is another variable declared,  secondWord , also of type  string . Write the statements needed so that the second word of the value of  sentence is assigned to  secondWord . So, if the value of  sentence were "Broccoli is delicious." your code would assign the value "is" to  secondWord . 

secondWord = sentence.substr (sentence.find ("`") + 1); secondWord = secondWord.substr (0, secondWord.find ("`"));


No comments:

Post a Comment