How to reverse the String in asp.net ?


Hi
In interview and in our daily work, so many time we will require the basic, like how to reverse the string in asp.net. we can do in a simple manner like this

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

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

}
protected void Button1_Click(object sender, EventArgs e)
{
string a = TextBox1.Text;
TextBox2.Text = reverse(a);

}

public string reverse(string s)
{
//take one new string
string str = string.Empty;
for (int j = s.Length – 1; j >= 0; j–)
{
//store the reverse character in seperate string
str = str + s[j];
}
return str;

}
}

Advertisements

10 Responses to “How to reverse the String in asp.net ?”

  1. krishnakumar Says:

    protected void Page_Load(object sender, EventArgs e)
    {
    string s = “Token A ^&*$@% Token B ^&*$@% Token C ^&*$@% Token D”;
    char[] a = s.ToCharArray();
    string s1 = string.Empty;
    for (int i = a.Length – 1; i >=0; i–)
    {
    s1 = s1 + a[i];
    lblmeg.Text = s1;

    }
    string[] b = s1.ToString().Split(‘ ‘);
    string s3 = string.Empty;
    string s4 = string.Empty;
    string s2 = string.Empty;
    for (int j = 0; j = 0; k–)
    {
    s3 = s3 + c[k];

    }
    TextBox1.Text = TextBox1.Text + s3;
    s2 = “”;
    }

    }

    }

    }

    • Chandra Dev Says:

      Hi

      why did you send this code ?

    • sudha Says:

      for loop will not work

  2. krishnakumar Says:

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

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    foreach (System.IO.DriveInfo drive in
    System.IO.DriveInfo.GetDrives())
    {
    TreeNode node = new TreeNode();
    node.SelectAction = TreeNodeSelectAction.SelectExpand;
    node.PopulateOnDemand = true;
    node.Expanded = false;
    node.Value = drive.Name;
    if (drive.IsReady)
    node.Text =
    String.Format(“{0} – (free space: {1})”,
    drive.Name, drive.AvailableFreeSpace);
    else
    node.Text =
    String.Format(“{0} – (not ready)”, drive.Name);
    this.TreeView1.Nodes.Add(node);
    }
    }
    }
    private void LoadDirectories(TreeNode parent, string path)
    {
    System.IO.DirectoryInfo directory =
    new System.IO.DirectoryInfo(path);
    try
    {
    foreach (System.IO.DirectoryInfo d in
    directory.GetDirectories())
    {
    TreeNode node = new TreeNode(d.Name, d.FullName);
    node.SelectAction = TreeNodeSelectAction.SelectExpand;
    node.Expanded = false;
    node.PopulateOnDemand = true;
    parent.ChildNodes.Add(node);
    }
    }
    catch (System.UnauthorizedAccessException ex)
    {
    parent.Text += ” (Access Denied)”;
    }
    catch (System.IO.IOException ex)
    {
    parent.Text +=
    string.Format(” (Unknown Error: {0})”, ex.Message);
    }
    }
    protected void TreeView1_TreeNodePopulate(object sender,
    TreeNodeEventArgs e)
    {
    LoadDirectories(e.Node, e.Node.Value);
    }
    protected void TreeView1_SelectedNodeChanged(object sender,
    EventArgs e)
    {
    try
    {
    System.IO.DirectoryInfo directory =
    new System.IO.DirectoryInfo(
    this.TreeView1.SelectedNode.Value);
    this.GridView1.DataSource = directory.GetFiles();
    this.GridView1.DataBind();
    }
    catch (System.UnauthorizedAccessException ex)
    {
    TreeView1.SelectedNode.Text += ” (Access Denied)”;
    }
    catch (System.IO.IOException ex)
    {
    TreeView1.SelectedNode.Text +=
    string.Format(” (Unknown Error: {0})”, ex.Message);
    }
    }

    }

    –%>

  3. krishna Says:

    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Windows.Forms;

    public partial class Registration : System.Web.UI.Page
    {
    SqlConnection con = new SqlConnection(“Data Source=KARTHIK-3CABF53;Initial Catalog=neathra;Integrated Security=True”);
    SqlCommand cmd = new SqlCommand();
    protected void Page_Load(object sender, EventArgs e)
    {
    con.Open();
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
    cmd = new SqlCommand(“sp_register”, con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue(“@cname”, txtuname.Text);
    cmd.Parameters.AddWithValue(“@caddress”, txtaddress.Text);
    cmd.Parameters.AddWithValue(“@cemail”, txtemail.Text);
    cmd.Parameters.AddWithValue(“@cmobile”, txtmobile.Text);
    cmd.Parameters.AddWithValue(“@date”, DateTime.Now.ToShortDateString());
    cmd.Parameters.Add(“@value”, SqlDbType.VarChar, 50);
    cmd.Parameters[“@value”].Direction = ParameterDirection.Output;
    cmd.ExecuteNonQuery();
    string output = Convert.ToString(cmd.Parameters[“@value”].Value);
    MessageBox.Show(output);
    Response.Redirect(“Registration.aspx”);

    }
    }

    create table register(cid int identity(100,1),cname nvarchar(30),caddress nvarchar(30),cemail nvarchar(30),cmobile nvarchar(15),date nvarchar(20))

    create proc sp_register
    (
    @cname nvarchar(30),
    @caddress nvarchar(30),
    @cemail nvarchar(30),
    @cmobile nvarchar(15),
    @date nvarchar(20),
    @value nvarchar (50) output
    )
    as
    if exists (select * from register where cname=@cname)
    begin
    set @value = ‘Already Existed’
    end
    else
    begin
    insert into register values(@cname,@caddress,@cemail,@cmobile,@date)
    set @value = ‘Inserted Successfully’
    end

  4. krishna Says:

    public void(int a,int b)
    {
    }

    • Chandra Dev Says:

      Hi
      what do you want to ask with me?

  5. krishna Says:

    Registration & Add New Item:
    IService:
    [ServiceContract]
    public interface IService
    {
    [OperationContract]
    void insert(customerdetails cd);
    [OperationContract]
    void additem(additemvariable aiv);

    }
    [DataContract]
    public class customerdetails
    {
    [DataMember]
    public string _cname,_caddress,_ccity,_cpincode,_cemail,_cphone,_cmobile;

    public string name
    {
    get
    {
    return _cname;
    }
    set
    {
    _cname=value;
    }
    }
    public string address
    {
    get
    {
    return _caddress;
    }
    set
    {
    _caddress=value;
    }
    }
    public string city
    {
    get
    {
    return _ccity;
    }
    set
    {
    _ccity=value;
    }
    }
    public string pincode
    {
    get
    {
    return _cpincode;
    }
    set
    {
    _cpincode=value;
    }
    }
    public string email
    {
    get
    {
    return _cemail;
    }
    set
    {
    _cemail=value;
    }
    }
    public string phone
    {
    get
    {
    return _cphone;
    }
    set
    {
    _cphone=value;
    }
    }
    public string mobile
    {
    get
    {
    return _cmobile;
    }
    set
    {
    _cmobile=value;
    }
    }
    }
    [DataContract]
    public class additemvariable
    {
    [DataMember]

    public string _itemname, _itemprice;

    public string itemname
    {
    get
    {
    return _itemname;
    }
    set
    {
    _itemname = value;
    }
    }
    public string itemprice
    {
    get
    {
    return _itemprice;
    }
    set
    {
    _itemprice = value;
    }
    }
    }

    Service:
    public class Service : IService
    {
    SqlConnection con=new SqlConnection(“Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Shroff\\Documents\\Visual Studio 2010\\WebSites\\InventryFinal\\App_Data\\Database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True”);
    SqlCommand cmd = new SqlCommand();
    public void insert(customerdetails cd)
    {
    con.Open();
    cmd = new SqlCommand(“sp_cdetails”, con);
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue(“@cname”, cd.name);
    cmd.Parameters.AddWithValue(“@caddress”, cd.address);
    cmd.Parameters.AddWithValue(“@ccity”, cd.city);
    cmd.Parameters.AddWithValue(“@cpincode”, cd.pincode);
    cmd.Parameters.AddWithValue(“@cemail”, cd.email);
    cmd.Parameters.AddWithValue(“@cphone”, cd.email);
    cmd.Parameters.AddWithValue(“@cmobile”, cd.email);
    cmd.Parameters.AddWithValue(“@cdate”, DateTime.Now.ToShortDateString());
    cmd.ExecuteNonQuery();

    }

    public void additem(additemvariable aiv)
    {
    con.Open();
    cmd = new SqlCommand(“sp_additem”, con);
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue(“@itemname”, aiv.itemname);
    cmd.Parameters.AddWithValue(“@itemprice”, aiv.itemprice);
    cmd.ExecuteNonQuery();
    }
    }

    ClassLibrary:

    namespace Customerregister
    {
    public class Class1
    {
    ServiceReference1.ServiceClient sc = new ServiceReference1.ServiceClient();
    ServiceReference1.customerdetails cds = new ServiceReference1.customerdetails();
    ServiceReference1.additemvariable aditv = new ServiceReference1.additemvariable();

    public void insert_customer(string cus_name, string cus_address, string cus_city, string cus_pincode, string cus_email, string cus_phone, string cus_mobile)
    {
    cds._cname = cus_name;
    cds._caddress = cus_address;
    cds._ccity = cus_city;
    cds._cpincode = cus_pincode;
    cds._cemail = cus_email;
    cds._cphone = cus_phone;
    cds._cmobile = cus_mobile;
    sc.insert(cds);
    }

    public void addnewitem(string itemname, string itemprice)
    {
    aditv._itemname = itemname;
    aditv._itemprice = itemprice;
    sc.additem(aditv);
    }
    }
    }

    Registration.aspx:

    Using customerregister;
    protected void btn_reg_Click(object sender, EventArgs e)
    {
    Class1 clas=new Class1();
    clas.insert_customer(txtusername.Text, txtaddress.Text, txtcity.Text, txtpin.Text, txtemail.Text, txtphone.Text, txtmobile.Text);

    AddnewItem.aspx:
    protected void Button1_Click(object sender, EventArgs e)
    {
    Class1 classes =new Class1();
    classes.addnewitem(txtitemname.Text,txtitemprice.Text);
    }

  6. sudha Says:

    problem with for loop

  7. rodney_mcswain Says:

    I every time used to study piece of writing in news papers but now as I
    am a user of net therefore from now I am using net
    for posts, thanks to web.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: