I am new at coding! Can someone help me with this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I am new at coding! Can someone help me with this code?

using System; public class Exercise24 { public static void Main() { string line = "Write a C# Sharp Program to display the following pattern using the alphabet."; string[] words = line.Split(new[] { " " }, StringSplitOptions.None); string word = ""; int ctr = 0; foreach (String s in words) { if (s.Length > ctr) { word = s; ctr = s.Length; } } Console.WriteLine(word); } } Hello! My request is someone to explain this code to me! I know for what is used for , but i can't grasp the Split Method.

4th Jan 2018, 7:21 AM
Кирил Величков
Кирил Величков - avatar
4 Answers
+ 9
Your Split() method splits (or breaks) the string 'line' from where the whitespaces are, because of the { " " } There are 10 whitespaces in the string 'line' and hence the array 'words' is an array of 10 elements (including "Write", "a", "C#", "Sharp", ...) Now with the foreach loop, you check the string of largest length in 'words' array (i.e. "following" - with a length of 9)
4th Jan 2018, 7:39 AM
Dev
Dev - avatar
+ 7
It specifies whether an array element that contains an empty string is included in the returned array. None is used in order to invoke the default behaviour of the Split method, which is to return an array of both empty substrings and substrings that aren't empty.
4th Jan 2018, 1:31 PM
Dev
Dev - avatar
+ 1
And for what is StringSplitOptions. None?
4th Jan 2018, 12:50 PM
Кирил Величков
Кирил Величков - avatar
0
Reallyy Thanks!
6th Jan 2018, 6:53 AM
Кирил Величков
Кирил Величков - avatar