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.
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:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework