Challenge - 5 Problems
String Mastery with nchar and substring
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nchar with special characters
What is the output of this R code?
R Programming
text <- "café" result <- nchar(text) print(result)
Attempts:
2 left
💡 Hint
Count the number of characters including accented letters.
✗ Incorrect
The nchar function counts characters, including accented ones as single characters. "café" has 4 characters.
❓ Predict Output
intermediate2:00remaining
Substring extraction with start and stop
What does this R code print?
R Programming
text <- "Programming" result <- substring(text, 4, 7) print(result)
Attempts:
2 left
💡 Hint
Remember substring counts characters starting at 1.
✗ Incorrect
substring(text, 4, 7) extracts characters 4 to 7: 'g','r','a','m' forming "gram".
🧠 Conceptual
advanced2:00remaining
Behavior of nchar with different encodings
Which statement about nchar in R is TRUE?
Attempts:
2 left
💡 Hint
Think about how nchar treats accented letters.
✗ Incorrect
nchar counts characters, not bytes, so accented letters count as one character each.
❓ Predict Output
advanced2:00remaining
Substring with start greater than stop
What is the output of this R code?
R Programming
text <- "DataScience" result <- substring(text, 5, 3) print(result)
Attempts:
2 left
💡 Hint
Check what happens if start is greater than stop in substring.
✗ Incorrect
If start > stop, substring returns an empty string "".
❓ Predict Output
expert2:00remaining
Combining nchar and substring for dynamic extraction
What does this R code print?
R Programming
text <- "HelloWorld" len <- nchar(text) result <- substring(text, len - 4 + 1, len) print(result)
Attempts:
2 left
💡 Hint
Calculate length and extract last 5 characters.
✗ Incorrect
nchar(text) is 10. substring(text, 6, 10) extracts characters 6 to 10: "World".