Reverse a char array in C#

12,976

Solution 1

Because every time you write the whole array not a single character, try this:

 foreach (char character in myChar)
 {
     Console.Write(character);
 }

Solution 2

 for( int i = myChar.Length -1 ; i >= 0 ; --i )
 {
      Console.Write(myChar[i]);
 }

Solution 3

There's no need to have a special array, do a reverse etc., just print out characters backward:

static void Main(string[] args) 
{
    Console.WriteLine("Enter text to be reversed");
    string inputText = Console.ReadLine();

    // Backward loop
    for (int i = inputText.Length - 1; i >= 0; --i)
      Console.Write(inputText[i]);
}
Share:
12,976

Related videos on Youtube

WindowsProdigy7
Author by

WindowsProdigy7

Hey guys I am a teenager that is enjoys coding, math and general logical thinking. I am currently trying to learn c# in my spare time so I can develop apps for Windows Phone 8! I also have a bit of background in C, HTML and CSS. Have a good day fellas!

Updated on October 15, 2022

Comments

  • WindowsProdigy7
    WindowsProdigy7 over 1 year

    I am trying to learn C#. I want to enter some text and for it to come out reversed. It reverses it, but multiple times, as many times as the inputted text is long. So hello comes out as olleholleholleholleholleh.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Reversed_Array
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter text to be reversed");
                string inputText = Console.ReadLine();
                char[] myChar = inputText.ToCharArray();
                Array.Reverse(myChar);
    
                foreach (char character in myChar)
                {
                    Console.Write(myChar);
                }
                Console.ReadLine();
            }
        }
    }
    

    I wanted to experiment with converting a string into a char array. Thought I would note this because yes I don't need the char array.

  • WindowsProdigy7
    WindowsProdigy7 over 10 years
    Wow, so simple how did I not see it. Thank you so much! I am new to this website and I love it already :D
  • WindowsProdigy7
    WindowsProdigy7 over 10 years
    This is a bit outside my skill level man! But thanks for the help!!
  • Guru Stron
    Guru Stron over 10 years
    @user3134332, was glad to help, see other answers, they suggest better approach for your example in total
  • WindowsProdigy7
    WindowsProdigy7 over 10 years
    Ahhh looked at it again man. This is great. I get it now just had to stare at it for a bit (Noobie here). Thanks for the answer!
  • Ankush Madankar
    Ankush Madankar over 10 years
    @WindowsProdigy7 If suggested solution solve ur problem then u should mark suggested solution as answer.
  • WindowsProdigy7
    WindowsProdigy7 over 10 years
    Marked as the answer now. Thanks!
  • Guru Stron
    Guru Stron over 10 years
    @bas =) one is great too)
  • Guru Stron
    Guru Stron over 10 years
    @WindowsProdigy7 again was glad to help, continue exploring)