check a given string is palindrome or not in C#.NET

 How to check a given string is palindrome or not in C#.NET?

check string palindrome or not

Fig.1 String Palindrome



Program Steps

  1. Get a string
  2. Reverse it.
  3. Compare the original string with Reverse string.
  4. If True then Palindrome other wise not a palindrome.

Input

INDIA


Output

INDIA

AIDNI

STRING IS NOT PALINDROME


Program

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

namespace CsharpProgram
{
    class Program
    {       
        public static void Main(string[] args)
        {           
       
            ReverseString("INDIA");
        }    
        private static void ReverseString(string str)
        {
            StringBuilder sb=new StringBuilder();
            Console.WriteLine("Before String Reverse....." + str);

            //Loop right to left
            
            for(int i=str.Length-1;i>=0;i--)
            {
                sb.Append(str[i]);
            }
 Console.WriteLine("After String Reverse......" + sb.ToString());
            
            //Compare two string to check palindrome

            if(str==sb.ToString())
                Console.WriteLine("String is Palindrome");
                else
                Console.WriteLine("String is not Palindrome");
        }

    }
}

OUTPUT

Before String Reverse.....INDIA
After String Reverse......AIDNI
String is not Palindrome



Share:

Strategy Design Pattern in C#

Strategy design pattern

Strategy design pattern comes under behavioral design pattern. It create different strategy classes which is interdependent to the Parent class, and these classes can be used interchangeably in side the family of parent class.  

Remember Always we should focus on future requirement and based on that we should design our application.

Book Definition

Strategy design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable, Strategy lets the algorithm very independently from client that use it.

Problem 

Lets say We have a video game where different kind of ducks do the different kind of operation.

Now Initially We have one abstract class called DUCK and one child class called ReadHeadedDuck. So we implemented inheritance concept and easily we achieve the functionality by reuse the code.

Its very easy right!! But there is some problem in it. we will discuss this below.

Now the game was more popular. So the owner of the video game thinking to add news features!!!!!!!

Now he added another object called RubberDuck.

Remember RubberDuck do not have all the functionality what ReadheadedDuck have. Then you need to forcefully implement the methods of DUCK in the RubberDuck even though its not required as it inheriting from parent DUCK. 

This violates DRY Principle.

See the below code.

 public abstract class Duck

{      

        public abstract void Swim();

        public abstract void Display();

        public abstract void FlyBird();

        public abstract void QuarkBird();              

    }

    public class RedHeadedDuck : Duck

    {

        public override void Display()

        {

            //Implement Display functionality

        }

        public override void FlyBird()

        {

            //Implement Fly functionality

        }

        public override void QuarkBird()

        {

            //Implement Quack functionality

        }

        public override void Swim()

        {

            //Swim functionality

        }

    }

    public class RubberDuck : Duck

    {

        public override void Display()

        {

            //Implement Display functionality

        }

        public override void FlyBird()

        {

            //Do not have Fly functionality

        }

        public override void QuarkBird()

        {

            //Do not have Quack functionality

        }

        public override void Swim()

        {

            //Implement Swim functionality

        }

    }

    

                                                                                                                                                                Lets say in future one more duck object will be added then we need

 to implement some method forcefully which is very difficult to 

maintain.

Solution

We should create a decoupled system which will help us to write less code and easy maintenance.

What should we do and how can we change the above code???

Step-1 Find the methods which can not be applicable to all the Objects(QuackBird() and FlyBird())

Step-2 Take those methods (Flybird(), Quackbird())and create a separate class called strategy class.

Step-3 Those strategy classes are used by the required objects of DUCK family. 

See the below design as described in the above 3 steps.




In the above Image DUCK is the abstract class which contains all the duck objects like ReadHeadedDuck,WhiteDuck and RubberDuck. 


See the below code which helps to maintain code if any future changes are there and also effort also less.

    class DesignPattern_Basic
    {
        public static void Main()
        {
            Duck redhead = new RedHeadedDuck();
            Duck white = new WhiteDuck();
            Duck rubber = new RubberDuck();
            redhead.Display();
            redhead.QuarkBird();
            redhead.FlyBird();
            redhead.Swim();
            Console.WriteLine("----------------------------");
            white.Display();
            white.QuarkBird();            
            white.Swim();
            Console.WriteLine("----------------------------");
            rubber.Swim();
            rubber.Display();                      
            Console.ReadKey();
        }

    }
    class flyHigh:IFlyable
    {
        public void fly()
        {
            Console.WriteLine("Duck flying very fast.....");
        }
    }
    public interface IFlyable
    {
        void fly();
    }
    public interface IQuackable
    {
        void quack();
    }
    class QuackHigh: IQuackable
    {
        public void quack()
        {
            Console.WriteLine("This Duck is quackable.....");
        }
    }
    public abstract class Duck
    {
        public IFlyable iflyable;
        public IQuackable iquack;
        public abstract void Swim();
        public abstract void Display();   
        public void FlyBird()
        {
            iflyable.fly();            
        }  
        public void QuarkBird()
        {
            iquack.quack();
        }
    }
    public class RedHeadedDuck : Duck
    {
        public RedHeadedDuck()
        {
            iflyable = new flyHigh();
            iquack = new QuackHigh();
        }
        public override void Display()
        {
            Console.WriteLine("This is redheaded duck");
        }
        public override void Swim()
        {
            Console.WriteLine("This duck swims fast");
        }
    }
    public class WhiteDuck:Duck
    {
        public WhiteDuck()
        {            
            iquack = new QuackHigh();
        }
        
        public override void Display()
        {
            
            Console.WriteLine("This is white duck");
        }
        public override void Swim()
        {
            Console.WriteLine("This duck swims slow");
        }
    }
    public class RubberDuck:Duck
    {      
        public override void Display()
        {
            Console.WriteLine("This is rubber duck");
        }
        public override void Swim()
        {
            Console.WriteLine("This duck swims slow");
        }
    }

Things to Remember
  • Strategy Design Pattern helps to create strategy class.
  • The Parent class not aware of the logic of the strategy class even though its part of the Parent class
  • Strategy class can be implemented to the family of parent class based upon the requirement.
  • Strategy design pattern is of behavioral type.



Thanks...........................
Share:

Azure Web Apps | web apps in azure

What is Azure Web Apps


Azure web apps is one of the most important service of Azure. It helps to deploy web application, Mobile application and API Services.

The main advantage is you no need to go to your Virtual machine to deploy your application. Simply web apps provides a platform to deploy directly. 

If the above theory is not clear then do not worry we will discuss briefly on web apps below........



Application deployment in Web App
Application deployment process in Web Apps

In the above we can see what are the steps to deploy web application to Azure Web apps.

How to deploy Application in Web Apps


Below steps shows how the application deployed.
  • Create a visual studio application
  • Create a Resource Group(If it is not exists)
  • Create an App service Plan(If it is not exists)
  • Create a web app in Azure 
  • Deploy the application on Web apps
  • Do the required configuration to show the page in the browser
Step -1 Create a visual studio application

You can create very simple asp.net application . It can be ASP.Net or MVC. Test your application whether its working fine or not.

Step -2 Create a Resource Group

Login Azure Portal --> Create a Resource -->Search Resource Group on the Search Bar -->Create --> Add a resource group name -->Press the button Review + Create

Step-3 Create an App Service Plan 

Login Azure Portal --> Create a Resource -->Search App service Plan on the Search Bar -->Create --> Fill the details  -->Press the button Review + Create

Remember:- Web Apps always runs under App Service Plan.

Step -4 Create a Web App

Login Azure Portal --> Go to Resource Group --> Add --> Search for Web App in the search bar --> Create as in the below Image.

Search Azure Web App
Search Azure Web App


Fill all the details -->Press the button Review + Create -->Create as shown below.



Create Azure Web App
Create Azure Web App


Step -5 Deploy the application in the Web app

Go to your application --> Right click and click Publish --> Start --> Select Azure App Service 


Publish Azure Web App
Publish Azure Web App

Select Azure  in the below Image

Publish Azure Web App
Publish Azure Web App-2

Select the Web required App in the below image


Azure Web App Selection
Web App Selection

Step -6 Do the required configuration to show the page in the browser.

If you are using MVC Application then no need to do any configuration changes in web app to run your application but in case of ASP.Net application you need to configure the start up page to run the application.

To do so Go to Web App -->  Configuration --> Default Document -->Add your page to the document.

We are done...................

Now we can able to see our application with the help of Web app URL. To see the URL we need to follow the below steps.
Go to Web App -->Overview -->URL


Share:

Right circular rotation of array in c# | How to perform Right circular rotation of an array



Right circular rotation

Here we will discuss one of the most important programming question i.e. Right circular rotation of array in c# or How to perform Right circular rotation of an array. The only thing we need to do is to shift each position one step right.

If you ant to know more logical programming please check Logical Programming section on top.

How to Perform Right circular rotation of an array?


Right-Circular-Rotation-Array
Right-Circular-Rotation-Array

 See the Above Image . It says Before Right Circular Rotation   1  3  5  7  9  11  where as after Right circular rotation 11 1 3 5 7 9.

Lets Understand the functional logic behind Right Circular Rotation.

 1  3  5  7  9  11   //Swap 1 and 3
 3  1  5  7  9  11  // swap 3 and 5
 5  1  3  7  9  11   // swap 5 and 7
 7  1  3  5  9  11  // swap 7 and 9
 9  1  3  5  7  11  // swap 9 and 11
11 1  3  5  7   9   //Final Output

In the above the Index position arr[0] will swap with arr[i+1] where i is the incremented value start with 0.

Program logic of Right circular rotation of array in c# 

temp=arr[0]
arr[0]=arr[i+1]  //where i=0,1,2,3....
arr[i+1]=temp

The above logic keep on repeating till the loop ended......

Lets understand the right circular rotation in programmatic way.


Right circular rotation of array in c#


 class Right_Rotation
    {
        public static void Main()
        {
            int[] arr = { 1, 3, 5, 7, 9, 11 };
            Right_Rotation_Method(arr);
            Console.ReadKey();
        }
        public static void Right_Rotation_Method(int[] arr)
        {
            Console.WriteLine("Before Right Circular Rotation....");
            foreach (var data in arr)
            {
                Console.Write(data + " ");
            }
            Console.WriteLine("");

            int temp_value = 0;
            for (int i = 0; i < arr.Length - 1; i++)
            {
                temp_value = arr[0];
                arr[0] = arr[i + 1];
                arr[i + 1] = temp_value;
            }
            Console.WriteLine("After Right Circular Rotation....");
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }
        }
    }

Output
Right-Circular-Rotation-array-Output
Right-Circular-Rotation-array-Output





Share:

Left rotation of array in c# | how to perform left circular rotation of an array in c#

Here I am going to discuss one of the most frequently asked question in Interview i.e. Left circular rotation of array in c# or How to perform Left circular rotation of an arrayThe only thing we need to do is to shift each position one step left.

If you ant to know more logical programming please check Logical Programming section on top.

Left rotation of array in c# | how to perform left circular rotation of an array

Left rotation of array in c# | how to perform left circular rotation of an array

Left rotation of array in c# | how to perform left circular rotation of an array












Example

Lets say i have an array of {10, 20, 30, 40, 50} then we need to shift one step left.

Input  -  10, 20, 30, 40, 50
Output - 20, 30, 40, 50, 10


Logic Behind Left Rotation of Array


10, 20, 30, 40, 50
10, 20, 30, 50, 40     //40 and 50 swapped
10, 20, 40, 50, 30    //40 and 30 swapped
10, 30, 40, 50, 20   //30 and 20 swapped
20, 30, 40, 50, 10  //20 and 10 swapped

How to find Left rotation of array in c# | how to perform left circular rotation of an array in c#?

class Left_Rotation
    {
        public static void Main()
        {
            int[] arr = { 10, 20, 30, 40, 50 };
            Left_Rotation_Method(arr);
            Console.ReadKey();
        }
        public static void Left_Rotation_Method(int[] arr)
        {
            int tempval;
            for (int i = arr.Length - 1; i > 0; i--)
            {
                tempval = arr[arr.Length - 1];
                arr[arr.Length - 1] = arr[i - 1];
                arr[i - 1] = tempval;
            }
            foreach (var item in arr)
            {
                Console.Write(item + " ");
            }
        }
    }


Output
Output of Left rotation of array in c#
Output of Left rotation of array in c#




Share:

service in angular 8 example | services in angular 8 tutorial | @Injectable() in angular 8

Service in angular 8 | @Injectable() in angular 8

Service is one of the most important part of angular which helps to define code which is accessible to multiple components in Angular.It reduced duplication of code which help to follow the Do not repeat Yourself(DRY) Principle.


Service in Angular 8
Service in Angular 8  



Why Service in Angular?

Lets discuss this with an example.

Lets observe the below Image.

Without Service In Angular
Without-Service-In Angular

In the Above Image we can see same JSON file for different component. It violates DRY Principle as discuss above section of this article.

What is the solution of it?

Think once if we can able to create one file and there we write JSON data and distribute among different component then we can reduce code as well as time. The code become cleaner and Understandable.

Now we got the solution, but whats our approach for that solution?

If we create a service in Angular and inject those service among different component then we can able to achieve it. 

So the below flow will be our approach .


With-Service-In Angular
With-Service-In Angular


We understood what is Service and Why we need Service.

Now We focus how we implement service in Visual studio.


Implementation


1.Lets create a folder as below as highlighted.



2. Under Testservice.component.ts write the below code.

import { Injectable } from '@angular/core';

@Injectable()
export class TestService {
    constructor() { }
    display() {
        return "display text!!!!";
    }
}

In the above @Injectable() is not not necessary but it is recommend to use this for future purpose.

Marking a class with @Injectable ensures that the compiler will generate the necessary metadata to create the class's dependencies when the class is injected.

3.Under Test.component.ts write the below code.

import { Component, Inject,OnInit } from '@angular/core';
import { TestService } from './Testservice.component';


@Component({
    selector: 'example-component',
    templateUrl: 'app/Test/Test.component.html',

})
export class ExampleComponent {
    text: string;
    constructor(private myService:TestService) {     
    }
    ngOnInit() {
        this.text = this.myService.display();
    }
}

In the above code we call the service method to get the required functionality.

4. Under Test.component.html write below code.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <b>
        {{text}}
    </b>
</body>
</html>

In the above we add the above code to display the data.

5. Add the Highlighted code in App.module.ts.

@NgModule({
    imports: [BrowserModule, FormsModule],
    declarations: [AppComponent, EmployeeComponent, EmployeeTitlePipe, mycomponentclass,
        CollegeDetailsComponent, CourseComponent, studentcomponent, MyEmployeeTitlePipe, ExampleComponent],
    providers: [TestService],
  bootstrap:    [ AppComponent ]
})

In the above code we need to register the service and component highlighted.

Run the application , you can able to see the output.
Share:

Method Decorator in Angular 8 | What is HostListener

What is Decorators



Decorators are a design that is used to separate modification or decoration of a class without modifying the original source code.

Decorators are of 4 types.


In this article we will discuss about Method decorator.


What is method decorator

Method decorator is declared just before method declaration.This can be used to observe,modify a method definition.

@HostListener is a good example of method decorator .
When any event happen in our host then we want decorated method to be called.


Method Decorator
Method Decorator


To implement @HostListener decorator we need to import HostListener Interface.


import { Component,HostListener} from '@angular/core';

Lets implement method decorator with an example.

Implementation

1.Create a folder called Test under App folder and add 2 files called Test.component.html and Test.component.ts.

2.Under Test.component.html  we need to add the below code.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <b>
        My Text
        <br />
        <br />
        Your Text
    </b>
</body>
</html>


3.Under Test.component.ts  we need to add the below code.

import { Component, HostListener } from '@angular/core';

@Component({
    selector: 'example-component',
    templateUrl: 'app/Test/Test.component.html',
})
export class ExampleComponent {
    @HostListener('click', ['$event'])
    OnHostClicked() {
        alert('You clicked the host');
    }
    @HostListener('mouseover', ['$event'])
    OnMouseOver() {
        alert('Mouse Over');
    }
}


In the above code we import HostListener interface.
Then added @HostListener('click', ['$event']) which allow the method to fire when click event occurs.

4. Write the below code under the App.component.ts .


<example-component></example-component>

5.Register the component class in app.module.ts.

We are done !!!!!!!!!
Run the application and you can see a alert once you mouse over the text.

Thanks............


Share:

Contact for Azure Training

Name

Email *

Message *

Subscribe YouTube

Total Pageviews

Popular Posts

Labels

Recent Posts