Thursday 7 March 2013

Input a string in a Textbox and it Reverse string in C#


Input a string in a textbox and reverse the string to display it in another textbox


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Reverse
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


//Using  by  for loop............................


char[] ch;
        private void btnByForLoop_Click(object sender, EventArgs e)
        {
            string ch = txtinputstring.Text;
            txtoutputstring.Text = string.Empty;
            for (int i = ch.Length - 1; i >= 0; i--)
            {
                txtoutputstring.Text += ch[i].ToString();

            }

        }
               
//Using  by  function ............................


        public string myrev(char[] val)
        {
            string total = string.Empty;
            for (int i = val.Length - 1; i >= 0; i--)
            {
                total += val[i].ToString();

            }
            return total;



        }



        private void BtnByFun_Click(object sender, EventArgs e)
        {
            string val = txtinputstring.Text;
            txtoutputstring.Text = myrev(val.ToCharArray());
        }


//Using  by ForEach Loop function ............................


        private void btnForEach_Click(object sender, EventArgs e)
        {
            foreach (char c in txtinputstring.Text.ToCharArray())
            {
                txtoutputstring.Text = c + txtoutputstring.Text;


            }
        }

    }
}







0 comments:

Post a Comment