Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

Wednesday, June 11, 2014

Code to Check whether Given Number or Sentence is Palindrome

Today i will tell you program in C#, about how to check whether a string is a Palindrome or not. You may also check Short - Three Statement long version.

What is Palindrome?
Palindrome is a word, phrase, number, or other sequence of symbols or elements that reads the same forward or reversed. For example: "madam", "1221". 

Here's the code:

using System;
namespace Palindrome_Checker
{
class Program
{
    static void Main(string[] args)
    {
        PalindromeChecker_class palindrome = new PalindromeChecker_class();
        Console.WriteLine("Enter phrase to be checked:");
        switch (palindrome.PalindromeChecker((Console.ReadLine())))
        {
            case true:
                {
                    Console.WriteLine("Yes, this phrase is Palindromic");
                    break;
                }
            case false:
                {
                    Console.WriteLine("No, this phrase is not Palindromic");
                    break;
                }
        }
        Console.ReadLine();
    }
}
class PalindromeChecker_class
{
    char[] phrase;
    bool ispalindrome = true;
    public bool PalindromeChecker(string phrase_to_be_checked)
    {
        phrase = phrase_to_be_checked.ToString().ToCharArray();
        switch (phrase_to_be_checked.Length)
        {
            case 1:
                {
                    return true;
                }
            case 2:
                {
                    return (phrase[0] == phrase[1]);
                }
            case 3:
                {
                    return (phrase[0] == phrase[2]);
                }
            default:
                {
                    for (int j = 0; ispalindrome && j < phrase.Length / 2; j++)
                    {
                        ispalindrome = (phrase[j] == phrase[phrase.Length - (j + 1)]);
                    }
                    return ispalindrome;
                }
        }
    }
}
 
}Some of the features of this Program are:
1. Input can be anything: a number, special characters, sentence in English or any other language.
2. It can check number and sentence of such a big length that when we assign that number or sentence to an array, the size of array does not exceed 2GB. (Since in .NET 4.5 "No object can have size greater than 2GB).

Hope you like it!

M.S. Saggoo is a Software Engineer and founder of , a popular  programming news and development blog since 2013.
What can I do for you? I can be found on  and can be reached by email .

Saturday, May 3, 2014

Precise Difference between Encapsulation and Abstraction

A priori, they've got nothing in common.
point x = { 1, 4 };
point y = { 23, 42 };

int d = distance(x, y);
Here, distance encapsulates the calculation of the (euclidean) distance between two points in a plane: it hides implementation details. This is encapsulation, pure and simple.
Abstraction is the process of generalization: taking a concrete implementation and making it applicable to different, albeit somewhat related, types of data. The classical example of abstraction is C's qsortfunction which sorts data.
The thing about qsort is that it doesn't care about the data it sorts – in fact, it doesn't know what data it sorts. Rather, its input type is a typeless pointer (void*) which is just C's way of saying “I don't care about the type of data” (this is also called type erasure). The important point is that the implementation of qsort always stays the same, regardless of data type. The only thing that has to change is the compare function, which differs from data type to data type. qsort therefore expects the user to provide said compare function as a function argument.
Encapsulation is hiding the implementation details which may or may not be for generic or specialized behavior(s).
Abstraction is providing a generalization (say, over a set of behaviors).

Wednesday, April 30, 2014

Get First 55203 ASCII Symbols

Language: Microsoft Visual C#

Hi Friends, today i will tell you code to get First 55,295 ASCIIs symbols stored in your computer. It can be also written in C++. This program even writes (and saves) all the values of ASCII symbols in a Microsoft WordPad file.

        static void Main(string[] args)
        {
            int i = 1;
            List<string> ascii_values = new List<string>();
            Console.WriteLine("How many First ASCII values do you want?");
            int max = Convert.ToInt32(Console.ReadLine());
            DateTime start_time = DateTime.Now;
            while (i <= max) { ascii_values.Add(i + " = " + Char.ConvertFromUtf32(i)); i++; }
            ascii_values.Add("\nTOTAL TIME ELEAPSED: " + (DateTime.Now - start_time).TotalMilliseconds.ToString() + " Milliseconds");
            ascii_values.Add("\nCreated ON: " +DateTime.Now.ToString());
            ascii_values.Add("\nDeveloped By Malwinder Singh");
            ascii_values.Add("\nVisit my Personal Blog: http://www.mssaggoo.blogspot.in");
            File.WriteAllLines(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\First " + max.ToString() + " ASCII Symbols.rtf", ascii_values.ToArray(), System.Text.Encoding.Unicode);
            System.Diagnostics.Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\First " + max.ToString() + " ASCII Symbols.rtf");
        } 


Console Output will be:


Enter value not more than 55203 and press Enter...

Program will stream list of these ASCII symbols to a word pad file which opens automatically and is saved in your desktop.
Output in Word file: