C
C#6mo ago
Eve.

✅ help. i'm trying to make the last letter the first letter and vice versa

No description
106 Replies
Pobiega
Pobiega6mo ago
so if I type hello it would produce oellh?
Eve.
Eve.OP6mo ago
no it just produces hello
Eve.
Eve.OP6mo ago
No description
Eve.
Eve.OP6mo ago
it should produce this yes
Pobiega
Pobiega6mo ago
right so lets break it down we need to be able to get the first and the last character, and "the middle" then we just combine them do you know how to get the first character?
Eve.
Eve.OP6mo ago
did i not get it by doing word[0]
Pobiega
Pobiega6mo ago
yep! what about the last?
Eve.
Eve.OP6mo ago
i used the for loop
Pobiega
Pobiega6mo ago
that seems silly, doesnt it?
Eve.
Eve.OP6mo ago
i tried to google it
Pobiega
Pobiega6mo ago
we only need the last - why would we just a loop
Eve.
Eve.OP6mo ago
would it be word[word.length] idk
Pobiega
Pobiega6mo ago
yeah that works there is a better way to do the same thing thou word[^1] the ^ means "start from the end"
Eve.
Eve.OP6mo ago
No description
Eve.
Eve.OP6mo ago
do i not need to convert it to an array
Pobiega
Pobiega6mo ago
strings are already arrays
Eve.
Eve.OP6mo ago
but when i do this
Pobiega
Pobiega6mo ago
yeah you cant use that for assigning
Eve.
Eve.OP6mo ago
No description
Pobiega
Pobiega6mo ago
you cant edit strings with indexing either and remove the loop you are trying to do everything in one go, which is just confusing do it one step at a time
var firstLetter = s[0]; // Just get the first letter
var lastLetter = s[^1]; // Equivalent to s[s.Length - 1]
var firstLetter = s[0]; // Just get the first letter
var lastLetter = s[^1]; // Equivalent to s[s.Length - 1]
we know these already, so lets use them now, how do we extract "the rest" of the string?
Eve.
Eve.OP6mo ago
does var mean string or int? or both ive never used var
Pobiega
Pobiega6mo ago
its a keyword that says "the type is obvious" it tells the compiler to pick the type for you
Pobiega
Pobiega6mo ago
No description
Eve.
Eve.OP6mo ago
No description
Pobiega
Pobiega6mo ago
screenshot from my IDE, where I have "type hints" enabled
Eve.
Eve.OP6mo ago
ohhh okay
Pobiega
Pobiega6mo ago
hm, are you using an old version of .NET? like, maybe .net framework?
Eve.
Eve.OP6mo ago
2022
Pobiega
Pobiega6mo ago
thats visual studio
Eve.
Eve.OP6mo ago
yes im using that yes
Pobiega
Pobiega6mo ago
visual studio is not the same thing as .NET $newproject
MODiX
MODiX6mo ago
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework. .NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended. https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
Pobiega
Pobiega6mo ago
you have accidentally created a project using the old legacy runtime for .NET that was discontinued in 2016 so its been stale for 8 years
Eve.
Eve.OP6mo ago
im using 6th one down
Pobiega
Pobiega6mo ago
yeah, see how its marked "NO" in big red letters?
Eve.
Eve.OP6mo ago
ive always used that one because it starts it off for me
Pobiega
Pobiega6mo ago
you should be using .NET not .NET Framework (I know, very confusing names)
Eve.
Eve.OP6mo ago
but the .NET doesn't give me the using system etc or the name space
Pobiega
Pobiega6mo ago
dont worry, thats just a template there is a way to undo it and get the "old" style program.cs back
Eve.
Eve.OP6mo ago
shall i just open a .NET and copy and paste this into it
Pobiega
Pobiega6mo ago
yeah that would work
Pobiega
Pobiega6mo ago
No description
Eve.
Eve.OP6mo ago
okay ill do that now
Pobiega
Pobiega6mo ago
see that box I marked with green? check it and you will get the more familiar program.cs
Eve.
Eve.OP6mo ago
No description
Pobiega
Pobiega6mo ago
yep!
Eve.
Eve.OP6mo ago
my exam is on monday though my coding one
Pobiega
Pobiega6mo ago
oh this is for school? did your teacher specify a specific version to learn?
Eve.
Eve.OP6mo ago
no im just practicing for it
Pobiega
Pobiega6mo ago
ok
Eve.
Eve.OP6mo ago
we have to moderate a code already given to us so im practicing how to manipulate things
Pobiega
Pobiega6mo ago
right well, create the new project and copy your stuff over
Eve.
Eve.OP6mo ago
okay ive done it and the ^1 is wokring so now i have the first and last characters
Pobiega
Pobiega6mo ago
yep now we need to get "the middle"
Eve.
Eve.OP6mo ago
would that not use the for loop and start from 1 instead of 0
Pobiega
Pobiega6mo ago
you could
Eve.
Eve.OP6mo ago
so i=1 instead
Pobiega
Pobiega6mo ago
but there is again a better solution
Eve.
Eve.OP6mo ago
okay im listening
Pobiega
Pobiega6mo ago
if you index a string with a range, you get whats called a substring word[x..y] what values do you think we should use for x and y?
Eve.
Eve.OP6mo ago
you mean data type or variable name?
Pobiega
Pobiega6mo ago
no I mean value I dont suggest you actually write x and y just type the "correct" values
Eve.
Eve.OP6mo ago
i dont understand
Pobiega
Pobiega6mo ago
word[0..2] for example
Eve.
Eve.OP6mo ago
erm 1..length-1
Pobiega
Pobiega6mo ago
right! and what is another way to type length-1?
Eve.
Eve.OP6mo ago
lastLetter-1
Pobiega
Pobiega6mo ago
^1
Eve.
Eve.OP6mo ago
^1-1
Pobiega
Pobiega6mo ago
no ^1
Eve.
Eve.OP6mo ago
wait so im taking out the last letter and the first letter to get the middle
Pobiega
Pobiega6mo ago
yeah
Eve.
Eve.OP6mo ago
okay
Pobiega
Pobiega6mo ago
var middle = s[1..^1]; essentially, take everything from position 1 to the last, minus 1, since the upper bound is exclusive
Eve.
Eve.OP6mo ago
okay so the .. means give me everything except what i stated
Pobiega
Pobiega6mo ago
ok so we know the three parts now we just need to combine them again
Eve.
Eve.OP6mo ago
so i make a new variable saying last + middle + first
Pobiega
Pobiega6mo ago
kinda but we're in a method right?
Eve.
Eve.OP6mo ago
yes
Pobiega
Pobiega6mo ago
so why not just return the "answer" directly?
Eve.
Eve.OP6mo ago
ahh okay wait but what would be the point in the method if im putting them back together in the main
Pobiega
Pobiega6mo ago
you wouldn't
Eve.
Eve.OP6mo ago
is this wrong
No description
Pobiega
Pobiega6mo ago
yes look at what you return
Eve.
Eve.OP6mo ago
ive changed it to word words*
Pobiega
Pobiega6mo ago
then its fine, but again, I would not create the wordsvariable at all I'd just return the answer
private static string FlipLetters(string s) => s[^1] + s[1..^1] + s[0];

private static string FlipLettersReadable(string s)
{
var firstLetter = s[0]; // Just get the first letter
var lastLetter = s[^1]; // Equivalent to s[s.Length - 1]
var middle = s[1..^1]; // Equivalent to s.Substring(1, s.Length - 2)

return $"{lastLetter}{middle}{firstLetter}";
}
private static string FlipLetters(string s) => s[^1] + s[1..^1] + s[0];

private static string FlipLettersReadable(string s)
{
var firstLetter = s[0]; // Just get the first letter
var lastLetter = s[^1]; // Equivalent to s[s.Length - 1]
var middle = s[1..^1]; // Equivalent to s.Substring(1, s.Length - 2)

return $"{lastLetter}{middle}{firstLetter}";
}
Eve.
Eve.OP6mo ago
what does the $ do?
Pobiega
Pobiega6mo ago
I included the "code golf" version here too, to show how small this code can be 🙂 thats string interpolation its a better way of combining things to strings, like... $"Hello {name}! Welcome to our shop. You have {coins} gold coins to spend." see how nice that reads?
Eve.
Eve.OP6mo ago
yes okay so now it returns them all together oh it works
Eve.
Eve.OP6mo ago
No description
Pobiega
Pobiega6mo ago
public static void Main()
{
Console.WriteLine("Enter a word:");
var word = Console.ReadLine()!;

var flipped = FlipLetters(word);

Console.WriteLine(flipped);
}
public static void Main()
{
Console.WriteLine("Enter a word:");
var word = Console.ReadLine()!;

var flipped = FlipLetters(word);

Console.WriteLine(flipped);
}
Eve.
Eve.OP6mo ago
which bit is not needed
Pobiega
Pobiega6mo ago
this is what I did
Eve.
Eve.OP6mo ago
oh okay yay thank you
Pobiega
Pobiega6mo ago
but yeah, you see how similar it is
Eve.
Eve.OP6mo ago
you've helped me more than my teachers
Pobiega
Pobiega6mo ago
sadly, that is often the case
Eve.
Eve.OP6mo ago
well thank you anyway can i add you so i can message you directly fine if not
Pobiega
Pobiega6mo ago
I don't help over DMs, just make a thread 🙂
Eve.
Eve.OP6mo ago
okay :)
Pobiega
Pobiega6mo ago
you can ping me inside that thread thou if you want
Eve.
Eve.OP6mo ago
okay will do thank you again
Pobiega
Pobiega6mo ago
this way, other people can read the thread and learn, or correct me if I say something wrong etc its better for everyone 🙂
Eve.
Eve.OP6mo ago
very true
Pobiega
Pobiega6mo ago
if you are "done" here, type /close to mark the thread as answered
Eve.
Eve.OP6mo ago
oki doki
Want results from more Discord servers?
Add your server