Complete the code to replace the first occurrence of 'cat' with 'dog' in the string.
result <- sub([1], "dog", "cat and cat")
The sub function replaces the first match of the pattern, so we use "cat" as the pattern to replace.
Complete the code to replace all occurrences of 'cat' with 'dog' in the string.
result <- gsub([1], "dog", "cat and cat")
sub instead of gsub for all replacements.The gsub function replaces all matches of the pattern, so the pattern should be "cat" to replace all occurrences.
Fix the error in the code to replace the first 'a' with 'o' in the string.
result <- sub([1], "o", "banana")
The pattern must be a string, so it needs to be in quotes like "a".
Fill both blanks to replace all digits with '#' in the string.
result <- gsub([1], [2], "Phone: 123-456-7890")
sub instead of gsub to replace all digits.The pattern "[0-9]" matches any digit, and "#" is the replacement character.
Fill all three blanks to replace all vowels with '*' in the string.
result <- gsub([1], [2], [3])
sub instead of gsub to replace all vowels.The pattern "[aeiouAEIOU]" matches all vowels, "*" is the replacement, and "Hello World" is the input string.