Challenge - 5 Problems
String Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of IndexOf and Substring
What is the output of this C# code snippet?
C Sharp (C#)
string text = "Hello, world!"; int index = text.IndexOf('o'); string result = text.Substring(index, 5); Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Remember that Substring's second parameter is length, not end index.
✗ Incorrect
IndexOf('o') returns 4 (the first 'o' in "Hello"). Substring(4,5) extracts 5 characters starting at index 4: 'o, wo'.
❓ Predict Output
intermediate2:00remaining
Extracting domain from email
What is the output of this C# code?
C Sharp (C#)
string email = "user@example.com"; int atPos = email.IndexOf('@'); string domain = email.Substring(atPos + 1); Console.WriteLine(domain);
Attempts:
2 left
💡 Hint
Substring starting after '@' extracts the domain part.
✗ Incorrect
IndexOf('@') finds position of '@'. Substring(atPos + 1) extracts everything after '@', which is 'example.com'.
❓ Predict Output
advanced2:00remaining
Using Regex to extract numbers
What does this C# code print?
C Sharp (C#)
using System.Text.RegularExpressions; string input = "Order1234: 56 items"; var match = Regex.Match(input, @"\d+"); Console.WriteLine(match.Value);
Attempts:
2 left
💡 Hint
Regex.Match finds the first sequence of digits.
✗ Incorrect
The pattern \d+ matches one or more digits. The first such sequence in the string is '1234'.
❓ Predict Output
advanced2:00remaining
Extracting substring between two characters
What is the output of this C# code?
C Sharp (C#)
string data = "[start]middle[end]"; int startIndex = data.IndexOf('[') + 1; int endIndex = data.IndexOf(']'); string extracted = data.Substring(startIndex, endIndex - startIndex); Console.WriteLine(extracted);
Attempts:
2 left
💡 Hint
Substring extracts from after '[' up to before ']'.
✗ Incorrect
IndexOf('[') returns 0, so startIndex is 1. IndexOf(']') returns 6. Substring(1, 5) extracts 'start'.
🧠 Conceptual
expert2:00remaining
Behavior of String.IndexOf with missing substring
What is the value of variable 'pos' after running this C# code?
C Sharp (C#)
string text = "hello world"; int pos = text.IndexOf("xyz");
Attempts:
2 left
💡 Hint
IndexOf returns -1 if substring is not found.
✗ Incorrect
When the substring is not found, IndexOf returns -1 instead of throwing an error or returning 0.