-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

About me

Hello

I'mITEngineer8995

Spatial Data Specialist

Do you want all of your codes in one place?? Then you are landed on the right blog... Here you will find varieties of codes of various languages starting from noob to advance with their samples. Do learn, execute and share your knowledge with others. Do visit my YouTube Channel for execution of these codes .

Experience

Video Clone Detector using Hadoop

2017-2018

A detector which differentiates copied videos from the original ones using Hadoop.

Portfolio

Java Program To Check Palindrome Using For Loop with charAt() function


Java Program To Check Palindrome Using For Loop with charAt() function

In this Java Program, we check palindrome using for loop with charAt() function.

A word, phrase, number or sequence of words that reads same backward as forward are known as palindromes.

E.g. madam, civic, level, refer, 121 1331 etc.

Logic : for (int i = length - 1; i >=0; i--)
            
                reverseString = reverseString + inputString.charAt(i);
                
Step 1 : Import java.util.Scanner

Step 2 : Ask the user to enter a string to check if it is a palindrome

Step 3 : Create variable length and store the length of the input string entered by the user.

Step 4 : Create variable reverseString

Step 5 : Apply the if loop.

Step 6 : Display the result.

Program :

import java.util.Scanner;

public class PalindromeForLoop 
    {
        public static void main(String[] args)
        {
            String reverseString="";
            Scanner sc = new Scanner(System.in);
            
            System.out.println("Enter a string to check if it is a palindrome:");
            String inputString = sc.nextLine();
            
            int length = inputString.length();
            
            for (int i = length - 1; i >=0; i--)
            
                reverseString = reverseString + inputString.charAt(i);
                
                if(inputString.equals(reverseString))
                    System.out.println("Input String is a Palindrome");
                else
                    System.out.println("Input String is not a Palindrome");
            }
        }
    
Output :

Enter any String : abccba
abccba
The input String is a Palindrome.

Enter any String : abc
abc
The input String is not a Palindrome.

For execution of the same do visit my YouTube Channel :





Java Program To Check Palindrome Using Queue


Java Program To Check Palindrome Using Queue

In this Java Program, we check palindrome using queue.

A word, phrase, number or sequence of words that reads same backward as forward are known as palindromes.

E.g. madam, civic, level, refer, 121 1331 etc.

A linear structure which follows a particular order in which the operations are performed is known as Queue.
It follows the First In First Out (FIFO) Order.

E.g. Queue of consumers for a resource where the consumers that came first will be served first.

Logic :

Step 1 : Import java.util.Queue

Step 2 : Import java.util.Scanner

Step 3 : Import java.util.LinkedList

Step 4 : Ask the user to enter the String by using the Scanner.

Step 5 : Create variable inputString and store the user entered string into it. The Data Type of the variable should be String as we are taking input from the user in the form of string.

Step 6 : Create a Linked List and store it in the Queue.

Step 7 : Apply the for loop

Step 8 : Reverse the String

Step 9 : Apply While loop

Step 10 : Display the output by applying the if else condition.

Program :

import java.util.Queue;
import java.util.Scanner;
import java.util.LinkedList;

public class PalindromeQueue 
    {
        public static void main(String[] args)
        {
            System.out.print("Enter any String:");
            Scanner sc = new Scanner(System.in);
            String inputString = sc.nextLine();
            Queue queue = new LinkedList();
            
            for(int i = inputString.length()-1; i >=0; i--)
            {
                queue.add(inputString.charAt(i));
               
            }
            
            String reverseString = "";
            
            while(!queue.isEmpty())
            {
                reverseString = reverseString+queue.remove();
                
            }
            if(inputString.equals(reverseString))
                System.out.println("The input String is a Palindrome");
            else
                System.out.println("The input String is not a Palindrome");
        }
    
}

Output :

Enter any String : abccba
abccba
The input String is a Palindrome.

Enter any String : abc
abc
The input String is not a Palindrome.

For execution of the same do visit my YouTube Channel :





Java Program To Check Palindrome Using Stack


Java Program To Check Palindrome Using Stack

In this Java Program, we check palindrome using stack.

A word, phrase, number or sequence of words that reads same backward as forward are known as palindromes.

E.g. madam, civic, level, refer, 121 1331 etc.

The linear data structure which follows a particular order in which the operations are performed is known as a stack. The order may be LIFO (Last In First Out) or FILO (First In Last Out).

E.g. Plates stacked over one another in a canteen.

Logic : Stack stack = new Stack();
            
            for (int i = 0; i < inputString.length(); i++)
            {
                stack.push(inputString.charAt(i));
          
            }
            
            String reverseString = "";
            
            while(!stack.isEmpty())
            {
                reverseString = reverseString+stack.pop();
                
            }

Step 1 : Import java.util.Stack

Step 2 : Import java.util.Scanner

Step 3 : Ask the user to enter any string by using Scanner

Step 4 : Create variable inputString and store the user's entered string in it.

Step 5 : Create constructor of stack

Step 6 : Apply for loop 

Step 7 : Apply while loop

Step 8 : Display the result using System.out.print

Program :

import java.util.Stack;
import java.util.Scanner;

public class PalindromeStack 
    {
        public static void main(String[] args)
        {
            System.out.print("Enter any string:");
            Scanner sc = new Scanner(System.in);
            String inputString = sc.nextLine();
            Stack stack = new Stack();
            
            for (int i = 0; i < inputString.length(); i++)
            {
                stack.push(inputString.charAt(i));
          
            }
            
            String reverseString = "";
            
            while(!stack.isEmpty())
            {
                reverseString = reverseString+stack.pop();
                
            }
            
            if(inputString.equals(reverseString))
                System.out.println("The input String is a palindrome");
            else
                System.out.println("The input String is not a Palindrome");
        }
    
}

Output :

Enter any String : level
The input string is a palindrome

For execution of the same do visit my YouTube Channel :



Java Program To Find Duplicate Characters In a String


Java Program To Find Duplicate Characters In a String

In this Java Program, we find the duplicate characters in a string by using HashMap. 

HashMap is made up of keys and values. In our program we are going to insert chars into keys and their counts into values  and get the set of duplicate keys in the string.

Logic: char are inserted as keys and their count as values

Step 1 : Import java.util.HashMap, java.util.Map, java.util.Set

Step 2 : Create class DupCharacters

Step 3 : Create a HashMap 

Step 4 : Convert the string to char array

Step 5 : Apply the logic and if map contains the char already then increase the value by 1

Step 6 : Apply the for loop

Step 7 : Obtain the set of keys

Step 8 : Display count of chars if it is greater than 1

Step 9 : Apply for loop 

Step 10 : Create the main method

Step 11 : Create the object of the DupCharacters class and display the result by using System.out.print method.

Program :

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

class DupCharacters 
    {
        public void countDupChars(String str)
        {
            //Create a HashMap
            Map<Character, Integer> map = new HashMap<Character, Integer>();
            
            //Convert the string to char array
            char[] chars = str.toCharArray();
            
            //logic: char are inserted as keys and their count as values
            //If map contains the char already then increase the value by 1
            
            for(Character ch:chars)
            {
                if(map.containsKey(ch))
                {
                    map.put(ch, map.get(ch)+1);
                }
                else
                {
                    map.put(ch,1);
                
                }
            }
            
            //Obtaining set of keys
            Set<Character> keys = map.keySet();
            
            //Display count of chars if it is greater than 1
            //All duplicate chars would be having value greater than 1
            
            for(Character ch:keys)
            {
                if(map.get(ch)>1)
                {
                    System.out.println("Char"+ch+""+map.get(ch));
                    
                }
            }
       
        }
        
        public static void main(String[] args)
        {
            DupCharacters obj = new DupCharacters();
            System.out.println("String: Welcome");
            System.out.println("---------------");
            obj.countDupChars("Welcome");
            
            System.out.println("\nString ITEngineer8995");
            System.out.println("-----------------------");
            obj.countDupChars("ITEngineer8995");
            
            System.out.println("\nString: YoutubeChannel");
            System.out.println("------------------------");
            obj.countDupChars("YoutubeChannel");
            
        }
}

Output:

String: Welcome
---------------
Chare2

String ITEngineer8995
-----------------------
Chare2
Charn2
Char92

String: YoutubeChannel
------------------------
Charu2
Chare2
Charn2

For the execution of the same do visit my YouTube Channel:


Java Program To Convert String To Char


Java Program To Convert String To Char

In this Java Program, we convert String to Char by using charAt() method

Logic :  char ch = str.charAt(i);

Step 1 : Create variable str and store the string "Hello" in it.

Step 2 : Use for loop by initializing i = 0; i < str.length(); i++

Step 3 : Create variable ch and store the result of the charAt() method

Step 4 : Display the result by using System.out.print method

Program :
               
public class StringToChar 
    {
        public static void main(String[] args)
        {
            //Using charAt() method
            String str = "Hello";
            for(int i = 0; i <str.length(); i++)
            {
                char ch = str.charAt(i);
                System.out.println("Character at "+i+"Position:"+ch);
           
            } 
              
        }
    
}

Output:

Character at 0 Position: H
Character at 1 Position: e
Character at 2 Position: l
Character at 3 Position: l
Character at 4 Position: o


For the execution of the same do visit my YouTube Channel:

Java Program To Convert Char To String


Java Program To Convert Char To String

In this Java Program, we convert char to string by using toString() and valueOf() methods.

Logic : Method 1: using toString() method
             String str = Character.toString(ch);

            Method 2: using valueOf() method
            String str2 = String.valueOf(ch);

Step 1 : For method 1, create variable ch and store character a in it.

Step 2 : Use the logic of method 1 and store it in the variable str.

Step 3 : Print the result by using System.out.print method

Step 4 : For method 2, use the logic of method 2 and store it in the variable str2.

Step 5 : Print the result by using System.out.print method

Program :

public class CharToString 
    {
        public static void main(String[] args)
        {
            //Method 1: using toString() method
            char ch = 'a';
            String str = Character.toString(ch);
            System.out.println("String is:"+str);
            
            //Method 2: using valueOf() method
            String str2 = String.valueOf(ch);
            System.out.println("String is:"+str2);
        }
    
}

Output:

String is : a
String is : a

For execution of the same do visit my YouTube Channel:


Java Program To Check Vowels and Consonants in a String


Java Program To Check Vowels and Consonants in a String

In this Java Program, we calculate the vowels and consonants in a string.

The alphabet contains 26 letters, among them A, E, I,  O, U (small and upper case) are known as vowels while the remaining 21 letters are known as consonants.  

Logic :  switch(ch)
            {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U': isVowel = true;
                  
            }
            if(isVowel == true)
            {
                System.out.println(ch+" is a Vowel");
                
            }
            else 
            {
                if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))
                    System.out.println(ch+" is a Consonant");
                else
                    System.out.println("Input is not an alphabet");
            }

Step 1 : Import java.util.Scanner

Step 2 : Create variable isVowel and initialize it to false

Step 3 : Ask the user to enter a character by using Scanner

Step 4 : Store the user entered character in the variable ch

Step 5 : Close the Scanner to avoid memory leak

Step 6 : Apply the logic by using the switch case

Step 7 : Apply if else condition 

Step 8 : Display the result by using System.out.print

Program :

import java.util.Scanner;

public class VowelConsonent 
    {
        public static void main(String[] args)
        {
            boolean isVowel = false;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter a character:");
            char ch = sc.next().charAt(0);
            sc.close();
            switch(ch)
            {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U': isVowel = true;
                  
            }
            if(isVowel == true)
            {
                System.out.println(ch+" is a Vowel");
                
            }
            else 
            {
                if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))
                    System.out.println(ch+" is a Consonant");
                else
                    System.out.println("Input is not an alphabet");
            }
        }
    
}

Output:

Enter a character :
A
A is a Vowel

Enter a character :
P
P is a Consonant

Enter a character :
9
Input is not an alphabet



For execution of the same do visit my YouTube Channel:

ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages