C
C#•10h ago
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.
C#
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();
C#
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
51 Replies
markiel
markiel•10h ago
What happens if you remove the else?
Angius
Angius•10h ago
does not print the shortest word
What happens instead?
It also messes up determining the position of the word cause adding them up gives the wrong result
If you mean the addingUp variable, then I have no idea what it's trying to achieve. It's adding two indexes, so if the shortest word is the 7th one and the longest word is the 4th one, your addingUp is 11 Which, I guess, can make some sense homework-wise, but not something I'd expect anybody do IRL
Kimo_Terapy
Kimo_TerapyOP•10h ago
it prints blank yes thats the point but it gives me the wrong number
mtreit
mtreit•10h ago
You have a fundamental flaw with your do-while loop.
Angius
Angius•10h ago
First of all, I'd use the debugger to see what words ends up being Maybe one of the words is an empty string or a null?
Kimo_Terapy
Kimo_TerapyOP•10h ago
perhaps, but you're suppose to end the questioning with a white space so how do I fix that so thats probably an issue it adds it to the index
mtreit
mtreit•10h ago
Think through what happens when the user enters whitespace. Yes.
Kimo_Terapy
Kimo_TerapyOP•10h ago
well its adding it to the index so I gotta remove it afterwards or something
mtreit
mtreit•10h ago
Don't add it in the first place.
Angius
Angius•10h ago
Or not add it in the first place
Kimo_Terapy
Kimo_TerapyOP•10h ago
right uhm is that even possible with my do while loop if statement ?
Angius
Angius•10h ago
if Yes
Kimo_Terapy
Kimo_TerapyOP•10h ago
aaaaaaa
Angius
Angius•10h ago
Simplest way to do it
Kimo_Terapy
Kimo_TerapyOP•10h ago
ok wait ima give it a shot
mtreit
mtreit•10h ago
Does the assignment require you use a do-while loop?
markiel
markiel•10h ago
I tried running your code with removed else
No description
Kimo_Terapy
Kimo_TerapyOP•10h ago
it works !!!! thank you sm guys 🫶 such a stupid mistake I didnt see on my own xd
markiel
markiel•10h ago
the loop is fine what did you changed?
Kimo_Terapy
Kimo_TerapyOP•10h ago
my do while
C#
do
{
Console.Write("Woord? ");
input = Console.ReadLine();

if (string.IsNullOrWhiteSpace(input))
{
break;
}
else
{
words.Add(input);
}

} while (!string.IsNullOrWhiteSpace(input));
C#
do
{
Console.Write("Woord? ");
input = Console.ReadLine();

if (string.IsNullOrWhiteSpace(input))
{
break;
}
else
{
words.Add(input);
}

} while (!string.IsNullOrWhiteSpace(input));
I just added an if statement to it
Angius
Angius•10h ago
With that break you could as well just use a while(true) loop tbh But this is fine as well
Kimo_Terapy
Kimo_TerapyOP•10h ago
yea I realized that as well
Angius
Angius•10h ago
Just some duplicated functionality
markiel
markiel•10h ago
oh, actually, I modified your code - with words.Where(w => !string.IsNullOrEmpty(w))
Kimo_Terapy
Kimo_TerapyOP•10h ago
wtf is Where lol
Angius
Angius•10h ago
LINQ Usually not allowed in homework, unless specifically discussing LINQ And only if the teacher knows it exists lmao
Kimo_Terapy
Kimo_TerapyOP•10h ago
yea ur right thank god the teachers made an autograder for the assignments
markiel
markiel•10h ago
oic
mtreit
mtreit•10h ago
I write almost all loops like this as while (true) and then break out as necessary so the loop condition doesn't have to be complex.
Kimo_Terapy
Kimo_TerapyOP•10h ago
i see
mtreit
mtreit•10h ago
while (true)
{
Console.Write("Woord? ");
input = Console.ReadLine();

if (string.IsNullOrWhiteSpace(input))
{
break;
}

words.Add(input);
}
while (true)
{
Console.Write("Woord? ");
input = Console.ReadLine();

if (string.IsNullOrWhiteSpace(input))
{
break;
}

words.Add(input);
}
However, what you have is also fine for this assignment I think.
markiel
markiel•9h ago
you can also do a post processing in the 2nd loop like if (string.IsNullOrEmpty(word)) continue; one liner
mtreit
mtreit•9h ago
That wastes memory! šŸ™‚
ćƒ†ć‚£ćƒŠ
ćƒ†ć‚£ćƒŠā€¢9h ago
you can check while reading the input, you dont need a list
Kimo_Terapy
Kimo_TerapyOP•9h ago
so much simpler lmao im obligated to use a list here xD i was obligated to use IndexOf(); as well
mtreit
mtreit•9h ago
Using a list is fine, especially since they want you to use the indexes.
markiel
markiel•9h ago
it won't matter šŸ˜„
Kimo_Terapy
Kimo_TerapyOP•9h ago
ah my autograde gave me my grade
Kimo_Terapy
Kimo_TerapyOP•9h ago
lets goo
No description
mtreit
mtreit•9h ago
For your next assignment, you might want to learn how to use a debugger to step through your code and inspect exactly what's happening. If you had done that for this assignment you would have been able to see pretty easily that the empty string was being added to the list and then could reason about how to prevent that.
Kimo_Terapy
Kimo_TerapyOP•9h ago
got it. You know any useful extensions for that or is that too unnecessary
mtreit
mtreit•9h ago
Are you using VSCode? Or Visual Studio?
Kimo_Terapy
Kimo_TerapyOP•9h ago
vs code i can use visual studio but then I gotta prove im a student at this college and what not
Angius
Angius•9h ago
Why?
mtreit
mtreit•9h ago
There is a free Community Edition of Visual Studio.
mtreit
mtreit•9h ago
I think debugging in Visual Studio is probably simpler, but if you want to try it with vscode there are instructions here: https://code.visualstudio.com/docs/csharp/debugging
Debugging
See how you can run and debug your C# source code
Kimo_Terapy
Kimo_TerapyOP•9h ago
alright ik more advanced programmers use visual studio but is it really that big of a difference im in my first year anyway
mtreit
mtreit•9h ago
For the kind of code you are writing it really should not matter much.
Kimo_Terapy
Kimo_TerapyOP•9h ago
next semester we're gonna learn MAUI
mtreit
mtreit•9h ago
But definitely learn how to use a debugger, it will help a lot.
Kimo_Terapy
Kimo_TerapyOP•9h ago
got it I will

Did you find this page helpful?