Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Find the longest word in a string

C# Sharp Basic: Exercise-24 with Solution

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

C# Sharp Exercises: Find the longest word in a string

Sample Solution:-

C# Sharp 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);
                    
    }
}

Test Data: Write a C# Sharp Program to display the following pattern using the alphabet.

Sample Output:

following 

Flowchart:

Flowchart: C# Sharp Exercises - Find the longest word in a string

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# program to convert a given string into lowercase.
Next: Write a C# program to print the odd numbers from 1 to 99. Prints one number per line.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.