How to find the smallest word in a string with C#

1 Answer

0 votes
using System;

class Program
{
    static void Main() {
        string str = "C# is a general purpose multi paradigm programming language";
        string[] words = str.Split(new[] {" "}, StringSplitOptions.None);
        string word = "";
        int min_len = 9999;
                        
        foreach (String s in words) {
            if (s.Length < min_len) {
                word = s;
                min_len = s.Length;
            }
        }
        Console.WriteLine(word);
    }
}



/*
run:
 
a
 
*/

 



answered Sep 14, 2021 by avibootz
...