Example Application: Finding a Palindrome



A palindrome is a word or a sentence that reads the same way from left to right or from right to left.

This is an example code of finding out whether a word or sentence is a palindrome. This code uses iteration:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Palindrome1
    public class Program
        // This method uses an iterative to analyze a string
        // and find out whether it is a palindrome
        private static bool Iterative(string strSubmitted)
            int fromLeft;
            bool result = false;
            int fromRight = strSubmitted.Length - 1;

            if (strSubmitted.Length == 1)
                result = true;
                for (fromLeft = 0;
                    fromLeft < strSubmitted.Length;
                    fromLeft++, fromRight--)
                    if (strSubmitted[fromLeft] == strSubmitted[fromRight])
                        result = true;
                        result = false;

            return result;

        static int Main(string[] args)
            // This variable will be used to hold the result of a method
            bool isPalindrome = false;
            // This is the value the user will enter
            string Sentence = "";
            // Request a string from the user
            Console.Write("Enter a word or a sentence: ");
            Sentence = Console.ReadLine();
            isPalindrome = Iterative(Sentence.ToLower());
            // Find out whether the user-entered string was a palindrome
            // Display the result accordingly
            if (isPalindrome == true)
                Console.WriteLine(Sentence + " is a palindrome.\n");
                Console.WriteLine(Sentence + " is not a palindrome.\n");
            return 0;

Home Copyright © 2009-2011 FunctionX