Question: Consider the following code, where the operations in the #braced area# of the while loop have been omitted: mystring
- Consider the following code, where the operations in the #braced area# of the while loop have been omitted:
mystring <- "R fever"
index <- 1
ecount <- 0
result <- mystring
while(ecount<2 && index<=nchar(mystring)){
# several omitted operations #
}
result
Your task is to complete the above code in the braced area so that it inspects mystring character by character until it reaches the second instance of the letter e or the end of the string, whichever comes first. The result object should be the entire character string if there is no second e or the character string made up of all the characters up to, but not including, the second e if there is one.
For example, mystring <- "R fever" should provide result as "R fev". This may be achieved by following these operations in the braces (or you can devise your own strategy):
- Use substr ( ) to extract the single character of mystring at position index.
- Use a check for equality to determine whether this single character string is either "e" OR "E". If so, increase ecount by 1.
- Next, perform a separate check to see whether ecount is equal to 2. If so, use substr to set result equal to the characters between 1 and index-1 inclusive.
- Increment index by 1.
Test your codeensure the previous result for mystring <- "R fever". Furthermore, confirm the following:
- Using mystring <- "beautiful" provides result as "beautiful"
- Using mystring <- "ECCENTRIC" provides result as "ECC"
- Using mystring <- "ElAbOrAte" provides result as "ElAbOrAt"
- Using mystring <- "eeeeek!" provides result as "e"
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
