Tuesday, July 29, 2014

Find the occurrence of Each character in an Given String

This Java code will Print the number of occurrence for all unique character in the given String

Input - Test@Tutor
Output  -
Occurrence of char [t] - 4
Occurrence of char [e] - 1
Occurrence of char [s] - 1
Occurrence of char [@] - 1
Occurrence of char [u] - 1
Occurrence of char [o] - 1
Occurrence of char [r] - 1


CODE -

public class CountCharOccurance
{
public static void main(String[] args)
{
String value = "Test@Tutor";

int[] count = new int[256];

value = value.toLowerCase();

for (int i = 0; i< value.length(); i++)
{
count[(int)value.charAt(i)]++;
}

List<Character> printed = new ArrayList<Character>();

for (int i = 0; i< value.length(); i++)
{
if (printed.contains(value.charAt(i)))
continue;

printed.add(value.charAt(i));

System.out.println("Occurrence of char [" + value.charAt(i) + "] - " + count[(int)value.charAt(i)]);
}
}

}

Converting ASCII to char and Char to ASCII

ASCII to Char
char char-value= (char) asciiValue;

Char to ASCII
int asciiValue = (int) char-Value

Input = 5245 the output should be displayed as 5000+200+40+5.

Java code to Format the given Integer Value

public class FormatIntValue
{
public static void main(String[] args)
{
int num = 5245;

int position = 1;
String outString = "";
String tempString = "";

while(true)
{
tempString = outString ;
outString = "";

outString += (num%10) * position;
position *= 10;
num /=10;

if (position != 10)
outString += "+";
outString += tempString;

if (num < 10)
break;

}
tempString = outString;
outString = "";

outString += (num)*position;
outString += "+";
outString += tempString;

System.out.println(outString);
}
}

Java code to print Asterisks (*) based on Input Provides

Java code to print Asterisks (*) based on Input Provides

e.g: n = 5
then o/p will be as -








CODE:  

public class AstrikPrint
{
public static void main(String[] args)
{
int n = 5;
int count = 1;
for (int i = 0; i<n/2; i++ )
{

for (int j = 0; j < count; j++)
{
System.out.print("*");
}
count+=2;

System.out.print("\n");
}

for (int j = 0; j < count; j++)
{
System.out.print("*");
}
count-=2;

System.out.print("\n");

for (int i = 0; i<n/2; i++ )
{
for (int j = count; j >0; j--)
{

System.out.print("*");
}
count-=2;

System.out.print("\n");
}

}


}

Tuesday, July 8, 2014

Find the Size of longest subsequent array (Subsequent array must be in increasing order)

This Java Code will find the Size of longest subsequent array (Subsequent arraywill be in increasing order)

Example:-

     Input array : { 10, 22, 9, 33, 21, 50, 41, 60, 80 }
     the largest subsequent array will be - {10, 22, 33, 50, 60, 80}
 So the Output will be - 6 

CODE:-

import java.util.Scanner;

public class Test {   

    public static int longestSeq(int[] input1) {
        int longest = 0;
        int[] longestArray = new int[input1.length];

        for (int i = 0; i < input1.length; i++) {

            longestArray[i] = 1;
            int tempCount = i ;
            for (int j = tempCount+1; j < input1.length; j++ ) {               

                if (input1[tempCount] < input1[j]) {

                    longestArray[i]++;
                    tempCount = j;
                }
            }
        }

        longest = longestArray[0];
        for (int i = 1; i<longestArray.length; i++) {

            if (longest < longestArray[i])
                longest = longestArray[i];
        }
        return longest;
    }

    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        System.out.println ("Enter Size of an array: ");
        int n = input.nextInt();


        int[] valueArray = new int[n] ;   

        System.out.println ("Enter The array elements: ");

        for (int i = 0; i< n; i++) {

            valueArray[i] = input.nextInt();
        }   

        System.out.println("The Longest Subsequent Array size is: " + longestSeq(valueArray));

    }

}
 

Tuesday, July 1, 2014

Find the second smallest number in an given array

This Java Code will print the Second Smallest number from the specified Array list

import java.util.Scanner;

public class Test {   

    public static int second (int[] values) {

        int min = -1, secondMin = -1;
        int firstValue = values[0];
        int secondValue = values[1];
        if (firstValue < secondValue) {
            min = firstValue;
            secondMin = secondValue;
        } else {
            min = secondValue;
            secondMin = firstValue;
        }
        int nextElement = -1;
        for (int i = 2; i < values.length; i++) {
            nextElement = values[i];
            if (nextElement < min) {
                secondMin = min;
                min = nextElement;
            } else if (nextElement < secondMin) {
                secondMin = nextElement;
            }
        }
        return secondMin;

    }

    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        System.out.println ("Enter Size of an array: ");
        int n = input.nextInt();


        int[] valueArray = new int[n] ;   

        System.out.println ("Enter The array elements: ");

        for (int i = 0; i< n; i++) {

            valueArray[i] = input.nextInt();
        }   

        System.out.println("Second Sortest element in given array is: " + second(valueArray));

    }

}

Open a default browser in Window machine through simple java Code

import java.io.IOException;

public class Test {   

    public static void main (String[] args) throws IOException {

        String url = "http://www.google.com";
        Runtime runtime = Runtime.getRuntime();

        runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
    }

}

Reverse the Given Integer Number

This java Code will reverse the given Integer number. (Integer number will be Ignoring any 0 value at beginning of Given Integer or Reversed integer value)

import java.util.Scanner;

public class Test {   
    public static int reverseNumber (int value) {

        int tempValue = 0;

        while (value > 9) {

            tempValue = tempValue*10;
            tempValue += value%10;
            value = value/10;

            if (value <= 9) {
                tempValue = tempValue*10;
                tempValue += value%10;
            }
        }
        value = tempValue;
        return value;
    }

    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        System.out.println("Enter number to reverse : ");
        int value = input.nextInt();

        System.out.println("Reversed Numver: " + reverseNumber(value));
    }

}

Reverse the alternate words from given string

This Java code will reverse the alternate words from a given String.
Example:





import java.util.Scanner;

public class Test {   

    public static String reverseAltWord(String value) {

        String[] tempValue = value.split("\\s");       

        for (int j=0; j<tempValue.length; j=j+2) {

            String curValue = tempValue[j];
            char[] valueList = new char[curValue.length()];

            int k = 0;
            for (int i = curValue.length() -1; i>=0; i--) {

                valueList[k] = curValue.charAt(i);
                k++;
            }

            tempValue[j] = String.valueOf(valueList);
        }

        value = "";
        for (int i=0; i<tempValue.length;i++) {

            value = value + tempValue[i] + " ";
        }
       
        return value;               
    }

    public static void main(String[] args) {

        String value ;

        Scanner input = new Scanner (System.in);

        System.out.println ("Enter String Value to be reversed: ");
        value = input.nextLine();

        String reversedString = reverseAltWord(value);

        System.out.println("Reverse2 String is : " + reversedString);       
    }
}

Reverse a given String

This Java code will take the Input String from Console by the user and prints the reversed string into the Console.

input: qwerty
output: ytrewq

import java.util.Scanner;

public class Test {   
   
   public static String reverseString(String value) {
       
        char[] valueList = value.toCharArray();
       
        int j = 0;
        for (int i = value.length()-1; i>=0; i--) {
           
            valueList[j] = value.charAt(i);
            j++;
        }
       
        value = String.valueOf(valueList);
       
        return value;               
    }
  
    public static void main(String[] args) {
       
        String value ;
       
        Scanner input = new Scanner (System.in);
       
        System.out.println ("Enter String Value to be reversed: ");
        value = input.nextLine();
   
        String reversedString = reverseString(value);

        System.out.println("Reverse2 String is : " + reversedString);       
    }
}