Skip to content

Instantly share code, notes, and snippets.

@StrykerKent
Last active August 13, 2018 03:11
Show Gist options
  • Save StrykerKent/b15d10d1a3d75a04badd31b3a89f30d9 to your computer and use it in GitHub Desktop.
Save StrykerKent/b15d10d1a3d75a04badd31b3a89f30d9 to your computer and use it in GitHub Desktop.
Question was "Find the largest word in a sentence." I used C# for this.
using System;
using System.Linq;
using System.Text.RegularExpressions;
class MainClass {
public static string LongestWord(string sen) {
// remove unwanted characters (everything except a-z 0-9 or spaces)
sen = Regex.Replace(sen, @"[^a-zA-Z0-9 ]+", "", RegexOptions.Compiled);
// split words into array
string[] words = sen.Split(' ');
// query for 1 word with largest length
var final = words.OrderByDescending(s=>s.Length).First();
return final;
}
static void Main() {
Console.WriteLine(LongestWord(Console.ReadLine()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment