You must Sign In to post a response.
  • Category: .NET

    Pallindrome program

    hi..
    i want a simple program of pallindome number and string in csharp.net.
    It will check the input is palindrome or not.
  • #583194
    try this code


    public static bool IsPalindrome(string strValue)
    {
    int intLen, intStrPartLen;
    intLen = strValue.Length - 1;

    //Cut the length of the string into 2 halfs
    intStrPartLen = intLen / 2;

    for (int intIndex = 0; intIndex <= intStrPartLen; intIndex++)
    {
    //intIndex is the index of the char in the front of the string
    //Check from behind and front for match
    if (strValue[intIndex] != strValue[intLen])
    {
    return false;
    }
    //decrease the lenght of the original string to
    //test the next Char from behind
    intLen--;
    }
    return true;
    }



    hope this will help you

  • #583210
    check this

    Client Side

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

    <!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></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblmsg" runat="server" Text=""></asp:Label><br />
    Enter Text to check palindrome or not  <asp:TextBox ID="txtPal" runat="server"></asp:TextBox><br /><br />
    <asp:Button ID="btnTest" runat="server" Text="Test" onclick="btnTest_Click" />
    </div>
    </form>
    </body>
    </html>


    Server Side

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

    public partial class Palindrom : System.Web.UI.Page
    {
    protected void btnTest_Click(object sender, EventArgs e)
    {
    Boolean res = CheckPalindrome(txtPal.Text);

    if (res)
    {
    lblmsg.Text = txtPal.Text + " is Palindrome";

    }
    else
    {

    lblmsg.Text = txtPal.Text + " is Not Palindrome";
    }
    }

    public static bool CheckPalindrome(string strValue)
    {
    int Len, PLen;
    Len = strValue.Length - 1;
    PLen = Len / 2;
    for (int intIndex = 0; intIndex <= PLen; intIndex++)
    {
    if (strValue[intIndex] != strValue[Len])
    {
    return false;
    }
    Len--;
    }
    return true;
    }

    }

    Regards
    N.Ravindran
    Your Hard work never fails

  • #583211
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace Palindrome
    {
    class P1
    {
    static void Main(string[] args)
    {
    string strVal=string.Empty ;
    Console.WriteLine("Enter the string to Check Palindrome");
    string s = Console.ReadLine();
    int i = s.Length;
    for (int j=i -1; j >= 0; j--)
    {
    str = str + s[j];
    }
    if (str == s)
    {
    Console.WriteLine(s + " is palindrome");

    }
    else
    {
    Console.WriteLine(s + " is not a palindeome"); }
    Console.WriteLine(strVal);
    Console.Read();
    }
    }
    }

    }

  • #583212

    using System;

    namespace PalindPgm
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class PalindromeTest
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    Console.WriteLine("Enter String");
    //get the input from the user
    string ipString = Console.ReadLine();
    //Create a object for the class to access method
    PalindromeTest c1 = new PalindromeTest();
    //Assign the result to the boolean variable.
    Boolean result = c1.PolinDrome(ipString);
    if (result == true)
    {
    Console.WriteLine("String is Palindrome");
    }
    else
    {
    Console.WriteLine("String is not Palindrome");
    }
    Console.Read();

    }

    //Function which is used to check a given string
    // is palindrome or not
    public bool PolinDrome(string usrInput)
    {

    int len = usrInput.Length;
    string strReverse = "";
    for (int i = len - 1; i >= 0; i--)
    {

    strReverse = strReverse + usrInput[i];
    }
    if (strReverse == usrInput)
    {
    return true;
    }
    else
    {
    return false;
    }

    }
    }

    }

    All is well.
    Thanks,
    Dharma
    Editor,Mentor,MVM
    Try and fail but don't fail and try

  • #583265
    Please check the following code for integer palindrome:


    protected void Page_Load(object sender, EventArgs e)
    {
    bool IsPalindrome = CheckPalindrome(1125431);
    if (IsPalindrome)
    {
    Response.Write("The given number is a Palindrome number");
    }
    else
    {
    Response.Write("The given number is not a Palindrome number");
    }

    }

    public bool CheckPalindrome(int InputNumber)
    {
    int temp, pal, sum = 0;
    temp = InputNumber;
    do
    {
    pal = InputNumber % 10;
    InputNumber = InputNumber / 10;
    sum = (sum * 10) + pal;
    }
    while (InputNumber != 0);
    if (temp == sum)
    return true;
    else
    return false;
    }


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #583295
    Hi,

    Private Sub Command1_Click()
    Dim st As String, rst As String
    st = InputBox("Enter any string")
    st = UCase(st)

    For i = Len(st) To 1 Step -1
    rst = rst & Mid(st, i, 1)
    Next i

    If rst = st Then
    Console.write "Palindrome"
    Else
    Console.Write "Not-Palindrome"
    End If
    End Sub



    Regards,
    Angad Yadav.


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.