We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Sunday, March 15, 2020

How Find the Largest Word in give string array in C#

Problem Description

Given a string of characters, find the largest word if two or more words are the largest then display the one that occurs first in the string.

Example:

String a ={ "a", "bb", "dddd", "cccc", "ffffff", "eeeee" };
Output = 
ffffff;

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: