Jump to content
  • Advertisement
Sign in to follow this  

Site for Code Discussion

This topic is 1216 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

I have often wanted to ask opinion on the basic exercises I do as part of me learning C# but I don't really feel such basic questions fit on this site so I was wondering if there were any other good places for such a thing.  

For example, I wrote a simple program to reverse a name, it works but I think it could be done tidier and was looking for feedback but as this site is mainly game dev (obv) I don't feel it has such a place.  

If I'm wrong of course please let me know and I'll happily ask away!

Share this post


Link to post
Share on other sites
Advertisement

Ok great, well I made a simple console program in C# to ask your name and repeat it or reverse it.  Although it achieves what I want it to (after a few tries) I was wondering if anything is glaringly bad or could be made in a lot less code.  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NameReverser
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter your name");
            string name = Console.ReadLine();
            Console.WriteLine("Would you like your name repeated(1) or reversed(2)");
            string choice = Console.ReadLine();

            if (choice == "1")
            {
                Console.WriteLine("How many times to repeat?");
                string numberofTimes = Console.ReadLine();
                int times = int.Parse(numberofTimes);

                for (int i = 1; i <= times; i++)
                  Console.WriteLine(i + " - " + name);                         
            }

            else if (choice == "2")
            {
                char[] array = name.ToArray();
                Array.Reverse(array);
                Console.WriteLine(array);                
            }
            else
            {
                Console.WriteLine("I do not understand, exiting...");
            }

            Console.ReadLine();            
        }
    }
}

Share this post


Link to post
Share on other sites

Forgot to add my clause that I haven't got to switch statements yet (although I'm aware of what they are used for).  I was actually a bit frustrated at just how difficult I found making such a simple program.  The main issue I had was forgetting how to do the parse bit.  I'm happy if there is nothing awful there anyway!

Share this post


Link to post
Share on other sites

Ah well I suppose I would add a conditional to make sure that times >=1.  As to the other thing I'm not sure where I would even start with that, I know I have the name stored in the array and I know I can find the size of it but that's a challenge for tomorrow I think :)  

Incidentally, I'm fairly sure when I was learning years ago I was told that a string was just a char array so I was surprised that I had to convert a string to an array in the first place.  I may have remembered this wrong of course (was reading C & C++)

Share this post


Link to post
Share on other sites

Ah well I suppose I would add a conditional to make sure that times >=1.  As to the other thing I'm not sure where I would even start with that, I know I have the name stored in the array and I know I can find the size of it but that's a challenge for tomorrow I think smile.png

 

You already have the chars put in an array. I think from there it should be obvious on how to go through it backwards wink.png

Edited by CC Ricers

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!