+ 1
Improve Efficiency/Clarity?
https://code.sololearn.com/cuRUfKW56t03/?ref=app I'm new to coding with anything other than MatLab, and I have started learning C#. I've been having a bit of trouble understanding how to evaluate/manipulate strings, but I was able to put this code together to take any words/letters input (including spaces) and output it with a reverse alphabet. It does work fine for the specific application I made it for. Could someone instruct me on possible ways to make this more efficient or cleaner? Or is this fine the way it is? P.S. I'm not worried about excluding special characters or numbers yet. I'll work on that later (but I wouldn't mind some tips on that, anyway 😉).
2 Respostas
+ 2
Your code works fine (at least it does the job). Modifying strings is not really memory-efficient, because strings are immutable. Well it is not really a big concern for such a small program.
What I recommend is try to write standalone functions that do a specific task, instead of putting everything in Main(). I usually only leave the IO stuff in the main (taking input and printing the result). That way you make your algorithm better testable, even with constant values.
Check this code to see how I would approach this.. I made one function that converts one character to another (MapChar) and another that applies this transformation on each character of a string and returns the result (Reverse). I used the Linq function Select, this may be a little advanced for you but Linq is generally a better and easier way to do anything where you would normally write a for loop, that's one of the best features of C# and I recommend learning it anyway :)
https://code.sololearn.com/c80tx27f22M8/?ref=app
+ 2
Tibor Santa, thanks so much for the response! That's actually super helpful and will definitely get me going in a good direction. I've found with coding that it's very difficult to find answers for specific situations when I don't know what to look for. The Linq stuff seems like it will be very helpful and I'll be looking into that for sure. Thanks again!