Friday, August 24, 2018

Thursday, August 23, 2018

Write a JAVA program for the given string and take another string and check whether the taken string is substring of given string or not.

/* Write a JAVA program for the given string and take another string and check whether the taken string is substring of given string or not.

Given String : google
Input String : gle
Output : gle is the substring of google
*/

 import java.util.Scanner;  
 class CheckSubstring  
 {  
     public static void main(String[] args)   
     {  
         String s="google";  
         Scanner sc=new Scanner(System.in);  
         System.out.println("Enter an String : ");  
         String a=sc.next();  
         boolean flag=false;int len=0;  
         for(int i=1;i<=s.length()-a.length();i++)  
         {  
             for(int j=0;j<a.length();j++)  
             {  
                 if(a.charAt(j)!=s.charAt(i+j) )  
                 {  
                     len=0;  
                     break;  
                 }  
                 else  
                     len++;  
             }  
             if(len==a.length())  
             {  
                 flag=true;  
                 break;  
             }  
         }  
         if(flag==true)  
             System.out.println(a+" is the substring of "+s);  
         else  
             System.out.println(a+" is not substring of "+s);  
     }  
 }  

How to find largest number less than a given number and without a given digit?

For example, If given number 165 and given digit is 6 then you should find the largest number less than 165 but it should not contain 4 in it. In this case  answer is 159.


package com.altafjava.test;

public class FindLargestNo {
public static void main(String[] args) {
int givenNum=165;
int givenDigit=6;
for(int i=givenNum;i>=Integer.MIN_VALUE;i--){
if((i+"").contains(givenDigit+"")==false){
System.out.println(i);
break;
}
}
}
}

Note :-  double quotes(" ") is used to convert primitive data type into String 





How to find largest number less than a given number and without a given digit?







How to count occurrences of each character of a string without using Regular Expression in java?

package com.altafjava.coos.test;

public class CountOccurrences2 {
public static void main(String[] args) {
String s="annotation";
System.out.println("Original String = "+s);

while(s.length()!=0){
int count=0;
for(int i=0;i<s.length();i++){
if(s.charAt(0)==s.charAt(i))
count++;
}
System.out.println(s.charAt(0)+" = "+count);
s=s.replaceAll(s.charAt(0)+"", "");
}
}
}




How to count occurrences of each character in a string in java?





How to count occurrences of each character of a string in java?

package com.altafjava.coocis.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CountCountOccurrences {
public static void main(String[] args) {
String s="annotation";
System.out.println("Original String = "+s);
while(s.length()!=0){
int count=0;
Pattern p=Pattern.compile(s.charAt(0)+"");
Matcher matcher=p.matcher(s);
while(matcher.find()){
count++;
}
System.out.println(s.charAt(0)+" = "+count);
s=s.replaceAll(s.charAt(0)+"","");
}
}
}




How to count occurrences of each character in a string in java?



Wednesday, August 22, 2018

How to find second largest number in an integer array?


package com.altafjava.sln.test;

public class SecondLargestNumber {
public static void main(String[] args) {

int[] arr={5,20,8,1,9,2,14,34,11};
int largest=Integer.MIN_VALUE;
int secondLargest=Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++){
if(arr[i]>largest){
secondLargest=largest;
largest=arr[i];
}
}
System.out.println("Original Array = 5,20,8,1,9,2,14,34,11");
System.out.println("Largest No = "+largest);
System.out.println("Second Largest No = "+secondLargest);
}
}



How to find second largest number in an integer array?






How to find sum of all digits of a number in java?



package com.altafjava.sad.test;

public class SumAllDigit {
public static void main(String[] args) {
int num=9526;
System.out.println("Original number = "+num);
int sum=0;
while(num!=0){
int remainder=num%10;
sum=sum+remainder;
num=num/10;
}

System.out.println("Sum = "+sum);
}
}






How to find sum of all digits of a number in java?
How to find sum of all digits of a number in java?




Tuesday, August 21, 2018

How to find duplicate elements in an array using HashSet?



package com.altafjava.fdeia.test;

import java.util.HashSet;

public class Test2 {

public static void main(String[] args) {
int[] arr={1,6,5,1,2,2,9,6};
System.out.println("Input Array = 1,6,5,1,2,2,9,6");
        System.out.print("The repeating elements are : ");
        HashSet<Integer> hashSet=new HashSet<>();
        for (int i = 0; i < arr.length; i++)
        {
        if(hashSet.add(arr[i])==false)
        System.out.print(arr[i]+" ");
        }       
}
}


As we know HashSet does not allow duplicate elements. We can get benifit from it. If we add element into HashSet and if it is returning false. It that means it element is already added HashSet and this is duplicate element.





How to find duplicate elements in an array using HashSet?
How to find duplicate elements in an array using HashSet?






How to find duplicate elements in an array?



package com.altafjava.fdeia.test;

public class Test {

public static void main(String[] args) {
int[] arr={1,6,5,1,2,2,9,6};
System.out.println("Input Array = 1,6,5,1,2,2,9,6");
        System.out.print("The repeating elements are : ");
        for (int i = 0; i < arr.length; i++)
        {
        for(int j=i+1;j<arr.length;j++){
        if(arr[i]==arr[j]){
        System.out.print(arr[i]+" ");
        break;
        }
        }
        }       
}
}


Note:-
We are using Brute Force Mechanism. This will take lot of time. Hence we can take the help of HashSet but in interview they check your logical skill.





How to find duplicate elements in an array?
How to find duplicate elements in an array?





Monday, August 20, 2018

Java Program to check two strings are Anagram or not



An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “LISTEN” and “SILENT” are anagram of each other.



package com.caots.test;

import java.util.Arrays;

public class Test {
public static void main(String[] args) {
//String s1="LISTEN", s2="SILENT";
String s3="TRIANGLE", s4="INTEGRAL";
boolean flag=checkAnagram(s3, s4);
if(flag==true)
System.out.println(s3+" & "+s4+" are Anagram strings");
else
System.out.println("No Anagram strings");
}

public static boolean checkAnagram(String s1,String s2){
char[] crr1=s1.toCharArray();
char[] crr2=s2.toCharArray();
Arrays.sort(crr1);
Arrays.sort(crr2);
if(Arrays.equals(crr1, crr2))
return true;
return false;
}
}







Java Program to check two strings are Anagram or not
Java Program to check two strings are Anagram or not