w3resource
C# Sharp Programming Exercises

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?



New Content: Composer: Dependency manager for PHP, R Programming