Finding longest word in string

后端 未结 4 1287
醉梦人生
醉梦人生 2021-01-19 04:13

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

相关标签:
4条回答
  • 2021-01-19 04:52

    Try this:

    var fileText = File.ReadAllText(@"C:\Users\RichardsPC\Documents\TestText.txt");
    var words = fileText.Split(' ')
    var finalValue = fileText.OrderByDescending(n=> n.Length).First();
    Console.WriteLine("Longest word: " + finalValue");
    
    0 讨论(0)
  • 2021-01-19 04:55

    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;
    }
    
    0 讨论(0)
  • 2021-01-19 05:00

    As suggested in the other answer, you need to split your string.

    string[] stringOfWords = fileText.split(new Char [] {',' , ' ' });
    //all is well, now let's loop over it and see which is the biggest
    int biggest = 0;
    int biggestIndex = 0;
    
    for(int i=0; i<stringOfWords.length; i++) {
        if(biggest < stringOfWords[i].length) {
            biggest = stringOfWords[i].length;
            biggestIndex = i;
        }
    }
    return stringOfWords[i];
    

    What we're doing here is splitting the string based on whitespace (' '), or commas- you can add an unlimited number of delimiters there - each word, then, gets its own space in the array.

    From there, we're iterating over the array. If we encounter a word that's longer than the current longest word, we update it.

    0 讨论(0)
  • 2021-01-19 05:01

    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
    
    0 讨论(0)
提交回复
热议问题