C# Programming Code Examples: Finding a Palindrome

In this guide I will show you how to create a program on Windows Forms which will help us to determine if the symbols that we input constitute a palindrome.

Let’s create a simple user interface:

finding-a-palindrome-using-c-1

Here we have a TextBox, Label, and Button. The message with the result of checking will be shown using MessageBox.

Double click the button “Check it” and go to the part of the code where we will perform the checking.

string stext = textBox1.Text;
char[] ctext = stext.ToCharArray();

First, we create the string for user’s input and write it to the “stext” variable. Then we create the char array to read the string symbol by symbol. Then we will be able to turn around our text.

Convert the text from string format to char using .ToCharArray method.
Turn around the text using:

Array.Reverse(ctext); 

Now we have a char array with reversed symbols. But we should compare strings, so we should convert it to string:

string resulttext = new string(ctext); 

So now we should just compare the inputted string with the one we turned around:

    if(stext == resulttext)
    {
    MessageBox.Show("This expression is a palindrome");
    }
    if (stext != resulttext)
    {
    MessageBox.Show("This expression is not a palindrome");
    }

The program is ready to use!

Screenshots:

finding-a-palindrome-using-c-2

finding-a-palindrome-using-c-3

Code listing:

Form1.cs:

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            string stext = textBox1.Text;
            char[] ctext = stext.ToCharArray();
            Array.Reverse(ctext);
            string resulttext = new string(ctext);
            if(stext == resulttext)
            {
                MessageBox.Show("This expression is a palindrome");
            }
            if (stext != resulttext)
            {
                MessageBox.Show("This expression is not a palindrome");
            }
            


        }
    }
}

Form1.Designer.cs:

namespace palindrom_wf
{
    partial class Form1
    {

        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(134, 13);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(229, 20);
            this.textBox1.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(83, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "Input the expression";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(20, 50);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(343, 43);
            this.button1.TabIndex = 2;
            this.button1.Text = "Check it!";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(374, 108);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Palindrome check";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button button1;
    }
}

Did you like one of our C# programming code examples? It was completed by a well-qualified expert in C# programming. If you need to get assignment help with C# feel free to contact AssignmentShark. Our experts are capable of completing any assignment examples and technical tasks. Just specify your requirements in detail and get timely help. When you use our service there’s no need to worry about your confidentiality. All your personal and financial information is kept in secret. Also you should know that you pay via independent payment system and you release payments after you get the order. Are you eager to work with us? Do not hesitate! Order assignment of the same quality as our C# programming code examples and increase your chances to get high scores!

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.