0
0
R Programmingprogramming~20 mins

nchar and substring in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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
intermediate
2:00remaining
Output of nchar with special characters
What is the output of this R code?
R Programming
text <- "café"
result <- nchar(text)
print(result)
A4
B5
C3
DError
Attempts:
2 left
💡 Hint
Count the number of characters including accented letters.
Predict Output
intermediate
2:00remaining
Substring extraction with start and stop
What does this R code print?
R Programming
text <- "Programming"
result <- substring(text, 4, 7)
print(result)
A"gram"
B"ogra"
C"ming"
D"Prog"
Attempts:
2 left
💡 Hint
Remember substring counts characters starting at 1.
🧠 Conceptual
advanced
2:00remaining
Behavior of nchar with different encodings
Which statement about nchar in R is TRUE?
Anchar counts bytes, so accented characters count as more than one.
Bnchar always returns the number of bytes, ignoring encoding.
Cnchar counts characters, and accented characters count as one character.
Dnchar returns the number of words in a string.
Attempts:
2 left
💡 Hint
Think about how nchar treats accented letters.
Predict Output
advanced
2: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)
AError
B"a"
C"ta"
D""
Attempts:
2 left
💡 Hint
Check what happens if start is greater than stop in substring.
Predict Output
expert
2: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)
A"Hello"
B"World"
C"oWorld"
D"orld"
Attempts:
2 left
💡 Hint
Calculate length and extract last 5 characters.