© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4mo ago•
86 replies
Kimo_Terapy

Determine positions of the longest and shortest word inside of a string list

It's for a school assignment. I tried everything and even CoPilot fails.

Th problem is that it prints the longest word, but does not print the shortest word. It also messes up determining the position of the word cause adding them up gives the wrong result.

Here is my current code, my naming is in dutch sorry.

List<string> words = new List<string>();
string input;

do
{
    Console.Write("Woord? ");
    input = Console.ReadLine();
    words.Add(input);

} while (!string.IsNullOrWhiteSpace(input));

string longWord = words[0];
string shortWord = words[0];

foreach (string word in words)
{
    if (word.Length < shortWord.Length)
    {
        shortWord = word;
    }
    else if (word.Length > longWord.Length)
    {
        longWord = word;
    }
}

int indexShortWord = words.IndexOf(shortWord);
int indexLongWord = words.IndexOf(longWord);
int addingUp = indexShortWord + indexLongWord;

Console.WriteLine($"Het langste woord is {longWord}");
Console.WriteLine($"Het kortste woord is {shortWord}");
Console.WriteLine($"De som van de posities is {addingUp}");

Console.ReadLine();
List<string> words = new List<string>();
string input;

do
{
    Console.Write("Woord? ");
    input = Console.ReadLine();
    words.Add(input);

} while (!string.IsNullOrWhiteSpace(input));

string longWord = words[0];
string shortWord = words[0];

foreach (string word in words)
{
    if (word.Length < shortWord.Length)
    {
        shortWord = word;
    }
    else if (word.Length > longWord.Length)
    {
        longWord = word;
    }
}

int indexShortWord = words.IndexOf(shortWord);
int indexLongWord = words.IndexOf(longWord);
int addingUp = indexShortWord + indexLongWord;

Console.WriteLine($"Het langste woord is {longWord}");
Console.WriteLine($"Het kortste woord is {shortWord}");
Console.WriteLine($"De som van de posities is {addingUp}");

Console.ReadLine();
So the point is, the player has to list as many words as possible, and when the person puts an empty space or whatever it is suppose to say what the longest word is in the list and the shortest word, and it's suppose to add up the position of the short and long word and print that as well.

I'm stuck idk
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Determine the positions of nodes inside of a graph
C#CC# / help
2y ago
string inside a string
C#CC# / help
4y ago
✅ Combining a List inside of a Select
C#CC# / help
3y ago
❔ List that has a list inside, and an identifier
C#CC# / help
3y ago