6

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.

3 Answers 3

12

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

 foreach (char character in myChar)
 {
     Console.Write(character);
 }
Sign up to request clarification or add additional context in comments.

6 Comments

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
@user3134332, was glad to help, see other answers, they suggest better approach for your example in total
@WindowsProdigy7 If suggested solution solve ur problem then u should mark suggested solution as answer.
Marked as the answer now. Thanks!
@bas =) one is great too)
|
2
 for( int i = myChar.Length -1 ; i >= 0 ; --i )
 {
      Console.Write(myChar[i]);
 }

2 Comments

This is a bit outside my skill level man! But thanks for the help!!
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!
1

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]);
}

6 Comments

Hey my man, I understand this after staring at it for it bit. Its printing out the character in the string by getting the amount of characters in the string minus the enter. Then going from highest value to lowest till it reaches 0. Would I be correct in saying this? This seems like a great method!
@WindowsProdigy7 not fully, -1 is because arrays in c# are zerobased, without -1 you'll just get IndexOutOfRangeException
I am a bit confused to why the -1 is needed. Wait is it because say I input hello which has a length of 5, but then because it starts at 0. h is 0, e is 1, l is 2, l is 3, and o is 4 so if it tried to print the 5th character there would be nothing there?
@WindowsProdigy7 if you try to print 5th element you'll be out of bounds of array(basically trying to access memory which belongs to something else, so you will get data which is incorrect and useless and if try to change it you'll corrupt other data) so you will get IndexOutOfRangeException to prevent possible errors and corrupt state
So in dumber term (sorry xD) I was right in saying that the string goes upto 4 but the length is 5. But because it starts at 0, there is nothing there when you try print 5. I need like a yes you are right or something otherwise I am never certian xD Thanks for helping!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.