0
0
C Sharp (C#)programming~10 mins

String searching and extraction in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the position of the first occurrence of "cat" in the text.

C Sharp (C#)
string text = "The cat is on the roof.";
int position = text.[1]("cat");
Console.WriteLine(position);
Drag options to blanks, or click blank then click option'
AReplace
BContains
CSubstring
DIndexOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using Contains instead of IndexOf returns a boolean, not a position.
2fill in blank
medium

Complete the code to extract the substring "dog" from the text.

C Sharp (C#)
string text = "The dog is barking.";
string animal = text.[1](4, 3);
Console.WriteLine(animal);
Drag options to blanks, or click blank then click option'
AIndexOf
BSubstring
CReplace
DContains
Attempts:
3 left
💡 Hint
Common Mistakes
Using IndexOf returns an integer, not a substring.
3fill in blank
hard

Fix the error in the code to check if the text contains the word "bird".

C Sharp (C#)
string text = "A bird is flying.";
bool hasBird = text.[1]("bird");
Console.WriteLine(hasBird);
Drag options to blanks, or click blank then click option'
AReplace
BIndexOf
CContains
DSubstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using IndexOf returns an integer, which needs comparison to check presence.
4fill in blank
hard

Fill both blanks to extract the substring after the first space in the text.

C Sharp (C#)
string text = "Hello World";
int spaceIndex = text.[1](' ');
string afterSpace = text.[2](spaceIndex + 1);
Console.WriteLine(afterSpace);
Drag options to blanks, or click blank then click option'
AIndexOf
BSubstring
CLastIndexOf
DReplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using LastIndexOf instead of IndexOf finds the last space, not the first.
5fill in blank
hard

Fill all three blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 3 characters.

C Sharp (C#)
string[] words = {"apple", "bat", "carrot", "dog"};
var lengths = words.ToDictionary([1] => [1], [2] => [2].Length).Where(kv => kv.Value [3] 3).ToDictionary(kv => kv.Key, kv => kv.Value);
foreach (var item in lengths) Console.WriteLine($"{item.Key}: {item.Value}");
Drag options to blanks, or click blank then click option'
Aword
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for key and value selectors causes errors.
Using '<' instead of '>' filters wrong words.