How To Reverse string In Asp.Net C#

How To Reverse string In Asp.Net C#

In this tutorial we will explain How To Reverse string In Asp.Net C#.Here we are taking two approach one is through inbuilt function and second is through programmatically.

Design View:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How To Reverse string In Asp.Net C#</title>
<style type="text/css">
body
 {
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 20px;
 }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>How To Reverse string In Asp.Net C#</h2>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>  
<br /><br />
<asp:Button ID="sub" Text="Reverse string" OnClick="sub_Click" runat="server" />
<br /><br />
<asp:Label ID="lbl1" runat="server" Text="Reverse string is:" Visible="false"></asp:Label>
<asp:Label ID="lbl" runat="server" Visible="false"></asp:Label>
<br /><br />
<br /><br />
All rights reserved by <a href="http://www.hightechnology.in">www.Hightechnology.in</a>
| Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>
</div>
</form>
</body>
</html>

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void sub_Click(object sender, EventArgs e)
    {
        string str = txt.Text;
        string strResult = string.Empty;
        int charCount = str.Count() - 1;
        for (int i = charCount; i >= 0; i--)
        {
            strResult += str[i].ToString();
        }
        lbl1.Visible = true;
        lbl.Visible = true;
        lbl.Text = strResult;
        
        //Or

        //char[] chararr = str.ToCharArray().Reverse().ToArray();
        //str = new string(chararr);
        //lbl.Text = str;

    }
}