Q:

Write a C# program to find the longest word in a string

0

Write a C# program to find the longest word in a string

Sample Output:

following

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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);
                    
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now