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

Common string methods in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of ToUpper and Trim methods
What is the output of this C# code snippet?
C Sharp (C#)
string text = "  Hello World  ";
string result = text.Trim().ToUpper();
Console.WriteLine(result);
AHELLO WORLD
BDLROW OLLEH
Chello world
DHello World
Attempts:
2 left
💡 Hint
Remember that Trim removes spaces at the start and end, and ToUpper converts all letters to uppercase.
Predict Output
intermediate
2:00remaining
Result of Replace and Substring methods
What will be printed by this C# code?
C Sharp (C#)
string s = "apple pie";
string newS = s.Replace("pie", "tart").Substring(0, 5);
Console.WriteLine(newS);
Atart
Bapplet
Capple pie
Dapple
Attempts:
2 left
💡 Hint
Replace changes "pie" to "tart" and Substring(0,5) takes the first 5 characters.
Predict Output
advanced
2:00remaining
Output of IndexOf and Contains methods
What is the output of this code?
C Sharp (C#)
string phrase = "Learning C# is fun!";
int index = phrase.IndexOf("C#");
bool hasFun = phrase.Contains("fun");
Console.WriteLine($"{index} {hasFun}");
A9 True
B9 False
C-1 True
D10 True
Attempts:
2 left
💡 Hint
IndexOf returns the first position of the substring or -1 if not found. Contains returns true if substring exists.
Predict Output
advanced
2:00remaining
Effect of ToLower and StartsWith methods
What does this code print?
C Sharp (C#)
string input = "OpenAI ChatGPT";
string lower = input.ToLower();
bool starts = lower.StartsWith("open");
Console.WriteLine(starts);
AFalse
Btrue
CTrue
Dfalse
Attempts:
2 left
💡 Hint
ToLower converts all letters to lowercase. StartsWith checks if string begins with given substring.
Predict Output
expert
3:00remaining
Output of complex string manipulation
What is the output of this C# code?
C Sharp (C#)
string s = "  CSharp Programming  ";
string result = s.Trim().Replace(" ", "_").ToLower();
Console.WriteLine(result);
ACSharp_Programming
Bcsharp programming
Ccsharp_programming
DCSharp Programming
Attempts:
2 left
💡 Hint
Trim removes spaces at ends, Replace changes spaces to underscores, ToLower makes all letters lowercase.