C# Reverse of numbers/ string when input is having extra spaces

C# - Reverse of a number array or string array when the input is not properly formatted. (Example having unnecessary extra spaces)

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

namespace NumberReverse
{
    class Program
    {
        static void Main(string[] args)
        {
            //Capture the input string even if it has improper spaces in between them.
            Console.WriteLine("Enter the sequence that needs to be reversed : ");
            string input = Console.ReadLine();
           
            string[] temp = input.Split(' ');
            string[] tempagain = new string[temp.Length];
            int finalarraylength = 0;

            //Try to remove all the unnecessary spaces into one space in between
            for (int i = 0; i < temp.Length; i++)
            {
                if (temp[i] != "")
                {
                    tempagain[finalarraylength] = temp[i];
                    finalarraylength++;
                }
            }         

            string[] final = new string[finalarraylength];

            Console.Write("Arranging them properly : ");
            for (int i = 0; i < finalarraylength; i++)
            {
                final[i] = tempagain[i];
                Console.Write(final[i] + " ");
            }

            Console.WriteLine();
            Console.Write("Reverse Order is :        ");
            string[] reverse = new string[final.Length];
            for (int i = 0; i < final.Length; i++)
            {
                reverse[(final.Length -1 ) - i] = final[i];
            }

            foreach (string s in reverse)
            {
                Console.Write(s + " ");
            }

            Console.ReadLine();
        }
    }
}

Output :

No comments: