on Leave a Comment

Java Program to Compare Two Strings

In java, we can compare two strings using some predefined methods namely, equals() and equalsIgnoreCase() method. Strings can also be compared using == operator.

Java program to compare strings by equals() method

public class CompareString{
   public static void main(String []args){
      String s1 = "pctechnicalpro";
      String s2 = "pctechnicalpro";
      String s3 = "Pctechnicalpro";
      String s4 = new String ("pctechnicalpro");
   
      //compare s1 with s2
      System.out.println(s1.equals(s2));
   
      //compare s1 with s3
      System.out.println(s1.equals(s3));
   
      //compare s1 with s4
      System.out.println(s1.equals(s4));
   }
}

Output:

true
false
true

equals() method return true if sequence of characters is same in both string objects, otherwise it returns false.

Java program to compare strings by equalsIgnoreCase() method

public class CompareString{
   public static void main(String []args){
      String s1 = "pctechnicalpro";
      String s2 = "pctechnicalpro";
      String s3 = "Pctechnicalpro";
      String s4 = new String ("pctechnicalpro");
   
      //compare s1 with s2
      System.out.println(s1.equalsIgnoreCase(s2));
   
      //compare s1 with s3
      System.out.println(s1.equalsIgnoreCase(s3));
   
      //compare s1 with s4
      System.out.println(s1.equalsIgnoreCase(s4));
   }
}

Output:

true
true
true

equalsIgnoreCase() method ignore the case and returns true if both string objects have same sequence of character, otherwise it returns false.






on Leave a Comment

Java Program to Find ASCII value of a Character

ASCII(American Standard for Information Interchange) is a code to represent English characters using numeric code. It includes upper and lower case English letters, numbers and punctuation symbols. It assigns a number to each character from 0 to 127. For example ASCII code for alphabet A is 65. In Java, we can find ASCII value of character by assigning a character value to integer variable.

Java Program to Find ASCII Value of Character

public class ShowAscii {

    public static void main(String[] args) {

        char ch = 'A';
        int asciiCode = ch;
        // we can also cast char to int
        int castascii = (int)ch;

        System.out.println("ASCII value of "+ch+" is: " + asciiCode);
        System.out.println("ASCII value of "+ch+" is: " + castascii);
    }
}

Output:

ASCII value of A is: 65
ASCII value of A is: 65


on Leave a Comment

Linear Search in Java

Linear search is a searching technique to search an element in array. We can also search element in array using binary search. Linear search is slower than binary search, so it is less used. But in linear search we don't need to sort an array while performing searching. So both searching technique have their own advantages and disadvantages.

In linear search, we traverse an array for searching specific element. If array is found, we note its index number and if item is not found till end, we display that "Element is not found".

Java program for linear search

/*Linear Search in Java*/
import java.util.Scanner;
public class LinearSearch{
 public static void search(int arr[], int item){
  int i, flag = 0;
  for(i = 0; i<arr.length; i++){
   if(arr[i] == item){
    flag = 1;
    System.out.println("Element "+arr[i]+" is found at index "+i);
   }
  }
  if(flag!=1)
   System.out.println("Element "+arr[i]+" is not found");
 }
 public static void main(String[] args){
  Scanner read = new Scanner(System.in);
  int []arr = new int[5];
  System.out.print("Enter Array Element (5 Elements): ");
  for(int i = 0; i<arr.length; i++){
   arr[i] = read.nextInt();
  }
  System.out.print("Enter the element to search: ");
  int item = read.nextInt();
  search(arr, item);
 }
}

Output:

Enter Array Element (5 Elements): 1 2 3 4 5
Enter the element to search: 3
Element 3 is found at index 2




on Leave a Comment

Binary Search in Java

Binary Search is a simple approach to search an element in array. If you are using binary search for searching element in array, then array must be sorted in ascending order. We can sort an array using sorting algorithms or by using Arrays.sort(arr) method. Binary search is faster than linear search. Binary search is also known as Half-Interval algorithm.

In binary search, we compares the given element with the middle element of array. If middle element is equal to given element then index of the middle element is returned. If given element is less than middle element, the algorithm again repeats on the sub-array to the left of the middle element and if the given element is greater than middle element, the algorithm again repeats on the sub-array to the right of the middle element.

Binary Search in Java

class BinarySearch{
	public static void binarySearch(int arr[], int start, int end, int key){
		int mid = (start + end)/2;  
		while( start <= end ){
			if ( arr[mid] < key ){
				start = mid + 1;     
			}
			else if ( arr[mid] == key ){
				System.out.println("Element "+key+" is found at index: " + mid);  
				break;  
			}
			else{
				end = mid - 1;
			}
			mid = (start + end)/2;  
                }  
		if ( start > end ){
			System.out.println("Element is not found!");  
		}  
	}  
	public static void main(String args[]){  
        int arr[] = new int[]{10,20,30,40,50};  
        int key = 40; 
        int end = arr.length-1;  
        binarySearch(arr,0,end,key);     
 }
}

Output:

Element 40 is found at index: 3

Binary Search in Java Using Recursion

class BinarySearch{
	public static int binarySearch(int arr[], int start, int end, int key){  
        if (end>=start){  
            int mid = start + (end - start)/2;  
            if (arr[mid] == key){  
            return mid;  
            }  
            if (arr[mid] > key){  
            return binarySearch(arr, start, mid-1, key);//search in left subarray  
            }
			else{  
            return binarySearch(arr, mid+1, end, key);//search in right subarray  
            }  
        }  
        return -1;  
    }  
    public static void main(String args[]){  
        int arr[] = {10,20,30,40,50};  
        int key = 50;  
        int end=arr.length-1;  
        int result = binarySearch(arr,0,end,key);  
        if (result == -1)  
            System.out.println("Element is not found!");  
        else  
            System.out.println("Element "+key+" is found at index: "+result);  
    }  
}

Output:

Element 50 is found at index: 4

Binary Search in Java Using Arrays.binarySearch()

binarySearch() is a method in Arrays class, so we need to import java.util package.

import java.util.Arrays;  
class BinarySearch{  
    public static void main(String args[]){  
        int arr[] = {10,20,30,40,50};  
        int key = 20;  
        int result = Arrays.binarySearch(arr,key);  
        if (result < 0)  
            System.out.println("Element "+key+" not found!");  
        else  
            System.out.println("Element "+key+" is found at index: "+result);  
    }  
}

Output:

Element 20 is found at index: 1