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.
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
What happens if you remove the
else
?does not print the shortest wordWhat happens instead?
It also messes up determining the position of the word cause adding them up gives the wrong resultIf 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 IRLit prints blank
yes thats the point but it gives me the wrong number
You have a fundamental flaw with your do-while loop.
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
?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
Think through what happens when the user enters whitespace.
Yes.
well its adding it to the index so I gotta remove it afterwards or something
Don't add it in the first place.
Or not add it in the first place
right uhm
is that even possible with my do while loop
if statement
?
if
Yesaaaaaaa
Simplest way to do it
ok wait ima give it a shot
Does the assignment require you use a do-while loop?
I tried running your code with removed
else

it works !!!!
thank you sm guys š«¶
such a stupid mistake I didnt see on my own xd
the loop is fine
what did you changed?
my do while
I just added an if statement to it
With that
break
you could as well just use a while(true)
loop tbh
But this is fine as wellyea I realized that as well
Just some duplicated functionality
oh, actually, I modified your code - with
words.Where(w => !string.IsNullOrEmpty(w))
wtf is Where
lol
LINQ
Usually not allowed in homework, unless specifically discussing LINQ
And only if the teacher knows it exists lmao
yea ur right thank god the teachers made an autograder for the assignments
oic
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.i see
However, what you have is also fine for this assignment I think.
you can also do a post processing in the 2nd loop like
if (string.IsNullOrEmpty(word)) continue;
one linerThat wastes memory!
š
you can check while reading the input, you dont need a list
so much simpler lmao
im obligated to use a list here xD
i was obligated to use
IndexOf();
as wellUsing a list is fine, especially since they want you to use the indexes.
it won't matter š
ah my autograde gave me my grade
lets goo

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.
got it. You know any useful extensions for that or is that too unnecessary
Are you using VSCode?
Or Visual Studio?
vs code
i can use visual studio but then I gotta prove im a student at this college and what not
Why?
There is a free Community Edition of Visual Studio.
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
alright
ik more advanced programmers use visual studio but is it really that big of a difference
im in my first year anyway
For the kind of code you are writing it really should not matter much.
next semester we're gonna learn MAUI
But definitely learn how to use a debugger, it will help a lot.
got it I will