Tuesday 9 August 2016

Write a program to display reverse of string in ASP.NET

This post will help you to demonstrate a program to display the reverse of a string in ASP.NET. You can copy this code after you open a blank project in IDE of ASP.NET(Visual Studio). Then try to run that copied code, at that time you will get an expecting output.

In this program we are trying to find reverse of a string that we entered in text-box. For this, we used one text-box and one label to print output, and a button to perform this function. There you can see these text-box and a button also in the output. But the label will be hidden, it will only visible after you try to find the output.Then enter any string that you want to check its reverse, after that click on the button. Then you will get the output. The output will be reverse of the string that you have entered in the text-box field. We hope this code will help you to be a developer or a programmer. Before copying, try to understand the method that we used to find the output. Click on the read more button to get this code.



Reverse.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="reverse.aspx.cs" Inherits="our_program.reverse" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>reverse of a string</title>
    <center><h1>reverse of string</h1></center>
</head>
<body>
    <form id="form1" runat="server">
    <center>
    <div>
    <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Button ID="btn" runat="server" Text="button" onclick="btn_Click" />
    <br />
    <br />
    <asp:Label ID="lbl1" runat="server" Text="result are shown here"></asp:Label>
    </div>
    </center>
    </form>
</body>
</html>

Reverse.aspx

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

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

        }

        protected void btn_Click(object sender, EventArgs e)
        {
   
       string str = txt1.Text;
       string strResult = string.Empty;
      
       int charCount = str.Count() - 1;
  
       for (int i = charCount; i >= 0; i--)
       {
           strResult += str[i].ToString();
       }
       lbl1.Text="reverse of "+str+" is "+strResult;
   }
  }
 }

No comments:

Post a Comment