Problem Description
Given a string of characters, find the smallest word if two or more words are the smallest then display the one that occurs first in the string.
Example:
String a ={ "a", "bb", "dddd", "cccc", "ffffff", "eeeee" };
Output = a;
Given a string of characters, find the smallest word if two or more words are the smallest then display the one that occurs first in the string.
Example:
String a ={ "a", "bb", "dddd", "cccc", "ffffff", "eeeee" };
Output = a;
class Example
{
public static void Main()
{
string[] str = new string[6] { "a", "bb", "dddd", "cccc", "ffffff", "eeeeee" };
string temp = "";
int a = 0;
for (int i = 0; i < str.Length; i++)
{
if (a==0)
{
temp = str[i];
}
else
{
if (a > str[i].Length)
{
temp = str[i];
}
}
a = temp.Length;
}
Console.WriteLine(temp);
Console.ReadLine();
}
}
Output:
No comments:
Post a Comment