Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding longest word in string

Tags:

c#

Ok, so I know that questions LIKE this have been asked a lot on here, but I can't seem to make solutions work. I am trying to take a string from a file and find the longest word in that string. Simples.

I think the issue is down to whether I am calling my methods on a string[] or char[], currently stringOfWords returns a char[].

I am trying to then order by descending length and get the first value but am getting an ArgumentNullException on the OrderByDescending method.

Any input much appreciated.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace TextExercises
{
    class Program
    {
        static void Main(string[] args)
        {
            var fileText = File.ReadAllText(@"C:\Users\RichardsPC\Documents\TestText.txt");
            var stringOfWords = fileText.ToArray();

            Console.WriteLine("Text in file: " + fileText);
            Console.WriteLine("Words in text: " + fileText.Split(' ').Length);

            // This is where I am trying to solve the problem
            var finalValue = stringOfWords.OrderByDescending(n => n.length).First();

            Console.WriteLine("Largest word is: " + finalValue);
        }
    }
}
like image 554
3therk1ll Avatar asked Dec 24 '22 03:12

3therk1ll


2 Answers

Don't split the string, use a Regex

If you care about performance you don't want to split the string. The reason in order to do the split method will have to traverse the entire string, create new strings for the items it finds to split and put them into an array, computational cost of more than N, then doing an order by you do another (at least) O(nLog(n)) steps.

You can use a Regex for this, which will be more efficient, because it will only iterate over the string once

var regex = new Regex(@"(\w+)\s",RegexOptions.Compiled);
var match = regex.Match(fileText);
var currentLargestString = "";

while(match.Success)
{
     if(match.Groups[1].Value.Length>currentLargestString.Length)
     {
         currentLargestString = match.Groups[1].Value;
     }

     match = match.NextMatch();
}

The nice thing about this is that you don't need to break the string up all at once to do the analysis and if you need to load the file incrementally is a fairly easy change to just persist the word in an object and call it against multiple strings

If you're set on using an Array don't order by just iterate over

You don't need to do an order by your just looking for the largest item, computational complexity of order by is in most cases O(nLog(n)), iterating over the list has a complexity of O(n)

var largest = "";
foreach(var item in strArr)
{
    if(item.Length>largest.Length)
        largest = item;
}
like image 50
konkked Avatar answered Jan 10 '23 23:01

konkked


Method ToArray() in this case returns char[] which is an array of individual characters. But instead you need an array of individual words. You can get it like this:

string[] stringOfWords = fileText.Split(' ');

And you have a typo in your lambda expression (uppercase L):

n => n.Length
like image 20
Martin Heralecký Avatar answered Jan 10 '23 23:01

Martin Heralecký