Coding Lab - TechOnTechnology

Program in C# to reverse a given string.

using System;
using System.Collections;

namespace Lab_Ex_10
{
    class String1
    {
        string str;
        public void ReadString()
        {
            Console.Write("\n Enter a string: ");
            str = Console.ReadLine();
        }
        public void Reverse()
        {
            char[] c = str.ToCharArray();
            string ts = string.Empty;           // string ts="";
            for (int i = c.Length - 1; i >= 0; i--)
                ts += c[i].ToString();
            Console.WriteLine("\n Reversed string without using STACK is : {0}", ts);      
        }
    }
    class String2
    {
        int index;
        ArrayList list;
        public String2()
        {
            index = -1;
            list = new ArrayList();
        }    
        public void Reverse()
        {
            Console.Write("\n Enter a string: ");
            string str = Console.ReadLine();
            for (int i = 0; i < str.Length; i++)
            {
                list.Add(str[i]);
                index++;
            }
            Console.Write("\n Reversed string using STACK is : " );
            while(index>=0)
            {
                object obj = list[index];
                list.RemoveAt(index);
                index--;
                Console.Write(obj);
            }        
        }
    }
    class ReverseString
    {
        public static void Main()
        {
            String1 S1 = new String1();
            String2 S2 = new String2();
            int ch, i=1;
            while(i==1)
            {
                Console.Clear();
                Console.WriteLine("\n ************************************");
                Console.WriteLine("\n Welcome to ReverseString Operations.");
                Console.WriteLine("\n ************************************");
                Console.WriteLine("\n 1-----> Reverse without using STACK");
                Console.WriteLine("\n 2-----> Reverse using STACK");
                Console.WriteLine("\n 3-----> EXIT");
                Console.WriteLine("\n ************************************");
                Console.Write("\n\n Enter your choice: ");
                ch = int.Parse(Console.ReadLine());

                switch (ch)
                {
                    case 1: S1.ReadString();
                            S1.Reverse();
                            break;
                    case 2: S2.Reverse();
                            break;
                    case 3: Environment.Exit(-1);
                            break;
                    default: Console.WriteLine(" Sorry !!! Wrong  choice.");
                            break;
                }
                Console.Write("\n\n Press ENTER to Continue: ");
                Console.ReadLine();           
            }
        }
    }
}

Name

Email *

Message *