Pages

Monday, April 8, 2013

C#: Find the largest string in an array

//declare a string as empty
string large = "";
//function to find largest string
public void FindLargest(string[] str)
{
foreach (string s in str)//for each string in string array
{
//compare the length of each string in an array
  if (s.Length > large.Length)
  {
     large = s;
   }
}
Console.WriteLine(large);
}


//for input string array as "hi","hello"
Output: hello

No comments:

Post a Comment