Thursday, 2 April 2015


PROGRAM 19 : ENTER A STRING TO FIND THE LONGEST WORD AND ITS LENGTH.

Example :



PROGRAM :

import java.util.Scanner;
public class wordlengthver2
{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string to find longest length:");
        String str = sc.nextLine();
        str = str + " "; // SPACE ADDED TO ENSURE STOPPAGE OF COMPARING AT THE END
        int m = 0 , p;
        String max = "" , c = ""; // CREATING TEMPORARY VARIABLES
        String min = str.substring(0 , str.indexOf(' ')); // EXTRACTS THE FIRST WORD OF STRING
        int l = min.length(); // FINDS ITS LENGTH
        char ch;
        for(int i = 0 ; i<str.length() ; i++){
            ch = str.charAt(i);
            if(ch!=' ') // CHECKING WHETHER EACH CHARACTER IS A WHITE SPACE OR NOT.
                c+=ch; // IF NOT SPACE THAN A WORD IS CREATED
            else{
               p = c.length(); // LENGTH OF THE EXTRACTED WORD.
               if(p>m){ // CHECKS IF THE LENGTH IS GREATER OR NOT
               m = p;
               max = c;
              }
              if(p<l){ // CHECKS IF LENGTH IS LESS
                min = c;
                l = p;
                }
               p = 0; // RESET TO ZERO FOR ANOTHER WORD
               c = "";
            }
        }
        System.out.println();
        System.out.println("LONGEST WORD IN STRING: " + max);
        System.out.println("LENGTH OF THE LONGEST WORD: " + m);
        System.out.println();
         System.out.println("SHORTEST WORD IN STRING: " + min);
        System.out.println("LENGTH OF THE SHORTEST WORD: " + l);
    }
}

Sunday, 29 March 2015

PROGRAM 18 : ENTER A STRING AND REMOVE THE DUPLICATE CHARACTERS OF STRING.

Example :


PROGRAM :

import java.io.*;
class RemoveDupChar
{
      public static void main(String args[])throws IOException
     {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any word : ");
        String s = br.readLine();
        int l = s.length();
        char ch;
        String ans="";
        for(int i=0; i<l; i++)
       {
            ch = s.charAt(i);
            if(ch!=' ')
                ans = ans + ch;
            s = s.replace(ch,' '); //Replacing all occurrence of the current character by a space
               }
        System.out.println("Word after removing duplicate characters : " + ans);
     }
}

 
PROGRAM 17 : ENTER A STRING AND REMOVE REPEATED CHARACTERS FROM STRING.

Example:


PROGRAM :

import java.util.Scanner;
class RemoveRepChar
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a word:");
        String s = sc.nextLine();
         s = s + " "; // Adding a space at the end of the word
         int l=s.length(); // Finding the length of the word
         String ans=""; // Variable to store the final result
         char ch1,ch2;
        for(int i=0; i<l-1; i++)
        {
            ch1=s.charAt(i);
            ch2=s.charAt(i+1);
            if(ch1!=ch2) // Check that consecutive characters are not equal
                ans = ans + ch1;
        }
        System.out.println("Word after removing repeated characters = "+ans); // Printing the result
    }
}

Saturday, 28 March 2015

PROGRAM 16 : ENTER A STRING AND ARRANGE THE CHARACTERS IN ALPHABETICAL ORDER,REMOVING ALL SPACES OF THE STRING.

Example:


 
 
PROGRAM :
 
 
import java.util.Scanner;
public class alphabeticalorder
{
   public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter a string:");
       String str = sc.nextLine();
       str = str.toUpperCase();
       int l = str.length();
       int i , j;
       char arr[] = new char[l]; // creating 'char' array to store the characters of string
       for(i = 0 ; i<l ; i++)
           arr[i] = str.charAt(i);// METHOD IS USED TO EXTRACT THE CHARACTERS OF STRING
       char t; // TEMPORARY VARIABLE CREATED
       for(i = 0 ; i<l ; i++) // SORTING OF CHARACTERS IN ASCENDING ORDER BY USING BUBBLE SORTING TECHNIQUE
       {
           for(j = 0 ; j<l-i-1 ; j++)
           {
               if(arr[j]>arr[j+1]) // COMPARING THE ELEMENTS OF ARRAY
               {
                   t = arr[j]; // SWAPPING MECHANISM
                   arr[j] = arr[j+1];
                   arr[j+1] = t;
                }
            }
        }
       String s = ""; //CREATE A NEW STRING TO REMOVE THE SPACES
       for(i = 0 ; i<l ; i++)
          s+=arr[i];
       s = s.trim(); // USING trim() method for eliminating the extra spaces
       System.out.println("OUTPUT:");
       System.out.println(s);
    }
}

PROGRAM 15 : ENTER THE NUMBER OF TERMS OF PELL SERIES TO BE PRINTED

(Pell series is series of numbers which starts with 1 and then 2 and goes on.
 The next term is sum of double of precedent number and the number before the precedent number.)

Example :


'5' is the sum of double of '2' and '1' , i.e. , (1 + (2x2)). This way the series goes on.


PROGRAM :

import java.util.Scanner;
public class pellseries
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of terms in pell series:");
        int n = sc.nextInt();
        int a = 1 , b = 2 , t = 3 , c;
        System.out.print(a + " " + b); // FIRST TWO TERMS ARE PRINTED
        while(t<=n)
        {
            c = a + (2*b.);  //THIRD TERM IS SUM OF THE TWICE VALUE OF THE PRECEDENT VALUE OF THAT OF THE VALUE PRECEDING THE PREVIOUS VALUE ALSO
            a = b; // SWAPPING VALUES
            b = c;
            System.out.print(" " + c);
            t++; // INCREASING THE NUMBER OF TERMS BY ONE
            }
        }
    }

 

PROGRAM 14 : TO CHECK WHETHER THE NUMBER ENTERED IS PART OF THE FIBONACCI SERIES.

(Fibonacci series starts with '0' and '1' and the next term is the sum of the previous two terms of series. It goes like this : 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21...................)

Example:


PROGRAM :

import java.util.Scanner;
public class isFibonacci
{
   public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter a number:");
       int n = sc.nextInt();
       if(n<0) // THE NUMBER ENTERED SHOULD BE EQUAL TO OR GREATER THAN ZERO
       {
           System.out.println("negative number entered.");
        }
        else
        {
            int a = 0 , b = 1 , c = 0;
            while(c<n){ // THE SERIES GOES ON TILL IT REACHES THE SEARCH ELEMENT OR EXCEEDS IT.
                c = a + b; // THIRD TERM IS EQUAL TO SUM OF PRECEEDING TWO VALUES
                a = b; // SWAPPING POSITIONS OF VALUE
                b = c;
            }
       if(c==n)
           System.out.println("Output : The number belongs to Fibonacci Series.");
       else
           System.out.println("Output : The number does not belong to Fibonacci Series.");
     }
   }
}

Friday, 27 March 2015

PROGRAM 13 : TO ACCEPT TWO DIFFERENT STRINGS AND SWAP THE VALUES WITHOUT USING THE CALL BY VALUE OR CALL BY REFERENCE MECHANISM.

(Swapping values means to interchange values of two different variables with each other.)

Example:


PROGRAM :

import java.util.Scanner;
public class swappingstrings
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the first string S1:");
        String s1 = sc.nextLine();
        s1 = s1.trim(); // REMOVES ALL EXTRA SPACES BEFORE AND AFTER THE STRING
        int len = s1.length();
        System.out.println("Enter the second string S2:");
        String s2 = sc.nextLine();
        s2 = s2.trim();
        System.out.println("------------------------------------------------");
        System.out.println("Value of s1 before swapping: " + s1);
        System.out.println("Value of s2 before swapping: " + s2);
        System.out.println("------------------------------------------------");
        s1 = s1 + s2; // WE ARE JOINING BOTH THE STRINGS
        s2 = s1.substring(0 , len); // USING STRING METHOD TO EXTRACT A PARTICULAR PART OF STRING
        s1 = s1.substring(len); // TO EXTRACT THE REMAINING PART.
        System.out.println("------------------------------------------------");
        System.out.println("Value of s1 after swapping: " + s1);
        System.out.println("Value of s2 after swapping: " + s2);
        System.out.println("------------------------------------------------");
    }
}