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




How do you check the equality of two inner arrays in java?




package com.ceo2a.test;

import java.util.Arrays;

public class Test2 {

public static void main(String[] args) {
int[] arr1={5,6,9};
int[] arr2={5,6,9};
Object[] objArr1={arr1};
Object[] objArr2={arr2};
System.out.println("objArr1 == objArr2 ? "+Arrays.equals(objArr1, objArr2));
System.out.println("objArr1 == objArr2 ? "+Arrays.deepEquals(objArr1, objArr2));
}
}





How do you check the equality of two inner arrays in java?
How do you check the equality of two inner arrays in java?





How do you check the equality of two arrays in java?




package com.ceo2a.test;

import java.util.Arrays;

public class Test {

public static void main(String[] args) {
int[] arr1={5,6,9};
int[] arr2={6,5,9};
int[] arr3={5,6,9};
System.out.println("arr1 == arr2 ? "+Arrays.equals(arr1, arr2));
System.out.println("arr1 == arr3 ? "+Arrays.equals(arr1, arr3));
}
}






How do you check the equality of two arrays in java?
How do you check the equality of two arrays in java?





Sunday, August 19, 2018

How to count duplicate characters in a string in java?




package com.cdcis.test;

/**
 * How to count duplicate characters in a string in java?
 *
 */
public class Test {

public static void main(String[] args) {
String s="altafjava.blogspot.com";
System.out.println("Original String = "+s);
int count=0;
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
for(int j=1;j<s.length();j++){
if(s.charAt(i)==s.charAt(j)){
count++;
}
}
System.out.println(s.charAt(i)+" = "+count);
count=0;
s=s.replaceAll(Character.toString(s.charAt(i)),"");
}
}
}






How to count duplicate characters in a string in java?
How to count duplicate characters in a string in java?




How to find duplicate characters in a string in java?




package com.fdcis.test;

/**
 * How to find duplicate characters in a string in java?
 *
 */
public class Test {

public static void main(String[] args) {
String s="altafjava.blogspot.com";
System.out.println("Original String = "+s);
System.out.print("Duplicate characters = ");
while(s.length()!=0){
char c=s.charAt(0);
if(s.indexOf(c,1)!=-1){
System.out.print(s.charAt(0)+" ");
}
if(s.charAt(0)=='.')
s=s.replaceAll("\\.","");
else
s=s.replaceAll(s.charAt(0)+"","");
}
}
}







How to find duplicate characters in a string in java?
How to find duplicate characters in a string in java?






How do you remove all white spaces from a string in java?




package com.rwsfs.test;

public class Test2 {

public static void main(String[] args) {
String s="Good Morning Mumbai";
StringBuilder sb=new StringBuilder();
for(int i=0;i<s.length();i++){
if(s.charAt(i)!=' '){
sb.append(s.charAt(i));
}
}
System.out.println("Original String = "+s);
System.out.println("\nNew String = "+sb);
}
}





How do you remove all white spaces from a string in java?
How do you remove all white spaces from a string in java?

How do you remove all white spaces from a string in java?




package com.rwsfs.test;

public class Test {

public static void main(String[] args) {
String s="My name is khan and i am not a terrorist";
String newString=s.replaceAll("\\s","");
System.out.println("Original String = "+s);
System.out.println("New String = "+newString);
}
}

How do you remove all white spaces from a string in java?
How do you remove all white spaces from a string in java?

Thursday, August 16, 2018

How to create a pyramid of numbers in java?




package com.npp.test;

public class Test {

public static void main(String[] args) {

int n=10;
for(int i=1;i<=n;i++)
for(int j=1;j<=n+1;j++)
System.out.print(i>9?j<=n-i+1?"  ":i+"  ":j<=n-i+1?"  ":i+"   ");
}
  System.out.println();
}



  Instead of writing ternery operator we can write this also

if(i>9)
    System.out.print(j<=n-i+1?"  ":i+"  ");
else
    System.out.print(j<=n-i+1?"  ":i+"   ");





How to create a pyramid of numbers in java?



How to reverse a string in java?



package com.rs.test;

public class Test {

public static void main(String[] args) {
String s="altafjava";
StringBuilder rs=new StringBuilder();
for(int i=s.length()-1;i>=0;i--){
rs.append(s.charAt(i));
}
System.out.println("Original String = "+s);
System.out.println("Reversed String = "+rs);
}
}







How to reverse a string in java?
How to reverse a string in java?