How to find the biggest word from a string using C #?

This is my code for finding the largest word in a given string. I have the length of all words in a line, how can I get the largest word to print? I tried to get all the biggest words, but can't do it with this help plz code?

using System;
using System.Linq; 
class largest1{
    public void largest(){
        Console.WriteLine("Enter the String:");

        string buffer1 = Console.ReadLine();
        string[] buffer = buffer1.Split(' ');
        int length;
        string largestword = buffer[0];

        for(int i = 0; i < buffer.Length; i++){
            string temp = buffer[i];
            length = temp.Length;

            if( largestword.Length < buffer[i].Length ) {
                largestword = buffer[i];
            }
        }

        var largestwords = from words in buffer
                            let x =  largestword.Length
                            where words.Length == x 
                            select words;

        Console.Write("Largest words are:");      
        foreach(string s in largestwords){
            Console.Write(s);
        }
    }

    static void Main(){
        largest1 obj = new largest1();
        obj.largest();
    }
}
+3
source share
8 answers
//Put this in your class so that it is persisted
string largestword = "";

//Put this right before your for loop
largestword = buffer[0];

//Put this inside your for loop
if( largestword.Length < buffer[i].Length ) {
     largestword = buffer[i];
}

EDITED: added support for multiple words with the same length

class LargestWordsClass
{
    public LargestWordsClass()
    {
        _LargestWords = new List<String>();
    }

    //This says that the variable can be set from the class but read by anyone
    public int LargestWordSize
    {
        get;
        private set;
    }

    //This lets users get the list without being able to modify it
    private List<String> _LargestWords;
    public String[] LargestWords
    {
        get
        {
            return _LargestWords.ToArray();
        }
    }

    public void FindLargestWord()
    {
        _LargestWords.Clear();

        Console.WriteLine("Enter the String: ");
        String buffer = Console.ReadLine();
        String[] splitBuffer = buffer.Split(' ');

        LargestWordSize = 0;
        for (int i = 0; i < splitBuffer.Length; i++)
        {
            if (LargestWordSize < splitBuffer[i].Length)
            {
                LargestWordSize = splitBuffer[i].Length;
                _LargestWords.Clear();
                _LargestWords.Add(splitBuffer[i]);
            }
            else if (LargestWordSize == splitBuffer[i].Length)
            {
                _LargestWords.Add(splitBuffer[i]);
            }

            Console.WriteLine("The word is " + splitBuffer[i] + " and the length is " + splitBuffer[i].Length.ToString());
        }

        Console.WriteLine("The largest word" + ((_LargestWords.Count > 1) ? "s are" : " is:"));
        for (int i = 0; i < _LargestWords.Count; i++)
        {
            Console.WriteLine(_LargestWords[i]);
        }
    }
}
+3
source

use max to get this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
    public static void Main() {
        string[] words = { "cherry", "apple", "blueberry" };

        int longestLength = words.Max(w => w.Length);

        Console.WriteLine("The longest word is {0} characters long.", longestLength);
    }
}
+5
source

, , LINQ, . (, IEnumerable<string>

        var ordered = test
            .GroupBy(str => str.Length)
            .OrderByDescending(grp => grp.Key)
            .First()
            .ToList();

:

  • , . IGrouping int, .
  • , ( )
  • .
  • (Optional) Project it into a list. You can simply use this IGroupingin a foreach loop without having to runToList()

Enjoy it!

+4
source

try it

public void largest(){

 Console.WriteLine("Enter the String:");
 string buffer1 = Console.ReadLine();
 string[] buffer = buffer1.Split(' ');
    int length;
    /*foreach (string word in buffer)
    {
    //Console.WriteLine(word);
    }*/
        string temp = string.Empty;
    for(int i = 0; i < buffer.Length; i++){
        if(temp.length = buffer[i].length)
            temp = buffer[i];         
}
Console.WriteLine("The word is " + temp + " and the length is " + temp.length.tostring());

}
0
source

Try the following:

string[] words = { "berryblue", "cherry", "apple", "blueberry" };
int maxLength = words.Max(s => s.Length);
var longestWords = from word in words where word.Length == maxLength select word;
foreach (var word in longestWords) System.Console.WriteLine(word);
0
source

Compressed version of linq:

static void Main(string[] args)
{
    Console.WriteLine("Enter the string:");
    string[] words = Console.ReadLine().Split(' ');
    var longestWords = words
        .Where(w => w.Length == words.Max(m => m.Length));
    Console.WriteLine("Longest words are: {0}", 
        string.Join(", ", longestWords.ToArray()));
}
0
source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Testingstring
{
    class Program
    {
        static void Main(string[] args)
        {
            string s,s2;
            s2 = " ";
            char[] a = new char[]{' '};
            Console.WriteLine("Enter any string");
            s = Console.ReadLine();

            foreach (string s1 in s.Split(a))
            {
                if (s2.Length < s1.Length)
                {
                    s2 = s1;
                }
            }
            Console.WriteLine("The largest string is " + s2 +" and its length is " +s2.Length);
            Console.ReadKey();



        }
    }
}
0
source

take a look at my solution

he uses Dictionaryto store wordstherelengths

class Program
{
    static void Main(string[] args)
    {
        var longestWord = LongestWord("Hello Qaru Welcome to Challenge World");

        PrintTheLongestWord(longestWord.Key);

    }

    public static KeyValuePair<string, int> LongestWord(String statement)
    {
        Dictionary<string, int> wordsLengths = InitializeDictionary(statement.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries));

        return GetMax(wordsLengths);
    }

    private static Dictionary<string, int> InitializeDictionary(string[] wordsList)
    {
        Dictionary<string, int> wordsLengths = new Dictionary<string, int>();

        foreach (var word in wordsList)
        {
            wordsLengths[word] = word.Length;
        }

        return wordsLengths;
    }

    private static KeyValuePair<string, int> GetMax(Dictionary<string, int> dictionary)
    {
        KeyValuePair<string, int> max = new KeyValuePair<string, int>(" ", Int32.MinValue);
        foreach (var item in dictionary)
        {
            if (item.Value.CompareTo(max.Value) > 0)
                max = item;
        }
        return max;
    }        

    public static void PrintTheLongestWord(string word)
    {
        Console.WriteLine($"the Longest word is {word} with length {word.Length}");
    }  
}

and here is another solution with one loop

 public static string LongestWord2(String statement)
    {
        string max = "";
        foreach (string word in statement.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries))
        {
            if (word.Length>max.Length)
            {
                max = word;
            }
        }
        return max;
    }
0
source

Source: https://habr.com/ru/post/1794319/


All Articles