Saturday, December 7, 2013

Java Program to check whether the words in a string are in ascending order or not.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class AscendingOrder {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.println(" Enter the String ");
        Boolean flag = true;
        String str = reader.readLine();
        for (int index = 0; index <= str.length() - 2; index++) {
            int check = (int) str.charAt(index) - (int) str.charAt(index + 1);
            if (check > 0)
                flag = false;

        }
        if (flag) {
            System.out.println(" Ascending Order");

        } else {
            System.out.println(" Not in Ascending Order");

        }

    }

}

Java Program to find the occurences of a digit in a number

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Occurences {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.println(" Enter the number ");
        int first = Integer.parseInt(reader.readLine());
        System.out.println(" Enter the digit to be searched ");
        int second = Integer.parseInt(reader.readLine());
        int count = 0, m;
        while (first > 0) {
            m = first % 10;
            if (m == second)
                count++;
            first = first / 10;
        }

        System.out.println("Number Of Occurences " + count);
    }
}

Java Program to find the number of occurences of a word in a string

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class WordOccurence {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.println(" Enter the  String ");
        String first = reader.readLine();
        System.out.println(" Enter the Word to be searched");
        String second = reader.readLine();
        int count = 0;

        String[] array = first.split(" ");
        for (int i = 0; i < array.length; i++) {
            if (array[i].equals(second))
                ++count;

        }

        System.out.println(" No. Of Occurences  " + count);
    }
}

Java Program to find nth term in a TRIBONACCI series

 // 0,1,1,2,4,7,13,.....


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Tribonacci {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.println(" Enter the number ");
        int num = Integer.parseInt(reader.readLine());
        int first = 0, second = 1, third = 1, nthTerm = 0;
        int num1 = num;
        if (num == 1)
            nthTerm = 0;

        if (num == 2 || num == 3)
            nthTerm = 1;

        while (num > 3) {
            nthTerm = first + second + third;
            // System.out.print(d +"\t");
            num--;
            first = second;
            second = third;
            third = nthTerm;
        }
        System.out.print(num1 + "th term of the Tribonacci is " + nthTerm);
    }
}

Java Program to seach a substring in a string

import java.io.*;

public class SearchKeyWord {

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

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nPlease enter product Name  :");

        int count = 0;

        boolean flag = false;

        String productName = reader.readLine();

        System.out.print("\nPlease enter Key word:");

        String keyword = reader.readLine();

        for (int index1 = 0; index1 < productName.length(); index1++) {

            for (int index2 = 0, index3 = index1; index2 < keyword.length(); index2++, index3++) {

                if (index3 < productName.length()) {

                    if (productName.charAt(index3) == keyword.charAt(index2)) {

                        flag = true;

                        count++;

                    }

                }

            }

            if (count == keyword.length()) {

                flag = true;

                break;

            }

            else {

                flag = false;

            }

            count = 0;

        }

        if (flag == true) {

            System.out.print("Yes, the key word is present in the product");

        }

        else {

            System.out.print("No, The key word is NOT present in the product");

        }

    }

}

Java Program to count prime numbers below a given number.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PrimeNumbers {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.println(" Enter the limit for the prime number ");
        int limit = Integer.parseInt(reader.readLine());
        int count = 0,total_count=0;
       
        for (int index1 = 1; index1 < limit; index1++) {
            for (int index2 = 1; index2 <= index1; index2++) {
                if (index1 % index2 == 0) {
                    count++;
                }
            }
            if (count == 2) {
                System.out.print(index1 + "\t");
                total_count++;

            }
            count = 0;
        }

        System.out.println();
        System.out.println(" Total Number of prime numbers = "+total_count);
    }

}

Java Programs to demonstrate use of String

1)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class UseOfString {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException{
        // TODO Auto-generated method stub
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print(" Enter the String ");
        String content=reader.readLine();
        for(int index=content.length()-1;index>=0;index--)
            System.out.print(content.charAt(index));
       
        System.out.println();
        System.out.println(" Enter thye String to be searched ");
        String subStr=reader.readLine();
        int location=content.indexOf(subStr);
        System.out.println(" The index is : "+location);
       
        location=content.indexOf(subStr, 5);
        System.out.println(" The index is : "+location);
             
        System.out.println(" UpperCase ::: "+content.toUpperCase());
        System.out.println(" LowerCase ::: "+content.toLowerCase());
       
       
    }

}

2)


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class UseOfString_1 {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException{
        // TODO Auto-generated method stub
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String source="";
        System.out.print(" Enter the 1st String ");
        source=reader.readLine();
        String subStr=source.substring(5);
        System.out.println(subStr);
       
        subStr=source.substring(6,8);
        System.out.println(subStr);
       
        System.out.println(" Enter the second string ");
        String destination=reader.readLine();
       
        System.out.println(source.equals(destination));
        System.out.println(source.equalsIgnoreCase(destination));
       
       
       
       
    }
}

3)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class UseString_2 {
   
    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println(" Enter the 1st String ");
        String first=reader.readLine();
        System.out.println(" Enter the 2nd String ");
        String second=reader.readLine();
        System.out.println(first.compareTo(second));
        System.out.println(first.compareToIgnoreCase(second));
       
        System.out.println(12+25+first+" "+second);
        System.out.println(first+" "+second+12+25);  // Once a string is encountered rest all are converted to string
       
        String cnvrt=12+" ";
        String srt="first-second-third";
        String arr[]=srt.split("-");
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
    }

}