Question
3. Consider the following code, where the operations in the #braced area# of the while loop have been omitted: mystring
3. 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):
a) Use substr ( ) to extract the single character of mystring at position index.
b) Use a check for equality to determine whether this single character string is either "e" OR "E". If so, increase ecount by 1.
c) 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.
d) 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started