Saturday, 22 October 2016

Find All Possible Combinations Of String In Java

import java.util.Scanner;

class SubstringsOfAString
{
   public static void main(String args[])
   {
      String string, sub;
      int  length;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter a string to print it's all substrings");
      string  = in.nextLine();

      length = string.length();  

      System.out.println("Substrings of \""+string+"\" are :-");

      for( int i = 0 ; i < length ; i++ )
      {
         for(int  j = 1 ; j <= length - i ; j++ )
         {
            sub = string.substring(i, i+j);
            System.out.println(sub);
         }
      }
   }
}

Output :

Substrings of "love" are :-
l
lo
lov
love
o
ov
ove
v
ve
e

Java Program to Convert Uppercase to Lowercase Letters and vice-versa with using String Functions

import java.util.*;

public class ChangeCase
{

 
    public static void changecase(String s)
    {
        for(int i=0;i<s.length();i++)
        {
            int ch=s.charAt(i);
            if(ch>=65&&ch<=90)
            {
                ch=ch+32;
                System.out.print( (char) ch);
            }
            else if(ch>=95&&ch<=122)
            {
                ch=ch-32;
                System.out.print( (char) ch);
            }
         }
    }
 
    public static void main (String args[])
    {
     
        Scanner scan = new Scanner(System.in);
     
        System.out.print("Enter a String : ");
        String s=scan.nextLine();

        ChangeCase.changecase(s);
     
    }
}

Java Program to find all Anagrams / Permutations of a given word in a Recursive format


import java.util.*;

class Anagrams
{
    int count = 0;
   
    void input()throws Exception
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a word : ");
        String s = sc.next();
        System.out.println("The Anagrams are : ");
        display("",s);
        System.out.println("Total Number of Anagrams = "+count);
    }
 
    void display(String s1, String s2)
    {
        if(s2.length()<=1)
        {
            count++;
            System.out.println(s1+s2);
        }
        else
        {
            for(int i=0; i<s2.length(); i++)
            {
                String x = s2.substring(i, i+1);
                String y = s2.substring(0, i);
                String z = s2.substring(i+1);
                display(s1+x, y+z);
            }
        }
    }
   
    public static void main(String args[])throws Exception
    {
        Anagrams ob=new Anagrams();
        ob.input();
    }
}


Output :

love

love
loev
lvoe
lveo
leov
levo
olve
olev
ovle
ovel
oelv
oevl
vloe
vleo
vole
voel
velo
veol
elov
elvo
eolv
eovl
evlo
evol
Total Number of Anagrams = 24   >
     

Java Program to find Frequency of Words in a String


import java.util.*;

public class WordsCounter1
{

 public static void main(String[] args)
 {
 
    String s;

        Scanner scan = new Scanner(System.in);
       
        System.out.print("Enter a String : ");
        s=scan.nextLine();

   countWords(s);

   }

  public static void countWords(String st)
   {
     int total = 0;
     String[] words=st.split(" ");
     int len = words.length;

     int[] fr=new int[len];

     
     for(int i=0;i<len;i++)
     {
       for(int j=0;j<len;j++)
       {
         if(words[i].equals(words[j]))
           {
             fr[i]++;

                }
            }
           }

     for(int i=0;i<len;i++)
       {
         for(int j=0;j<len;j++)
         {
           if(words[i].equals(words[j]))
           {
             if(i!=j)
               words[i]="";

           }
          }
         }



System.out.println("Duplicate words:");
for(int i=0;i<len;i++)
{
if(words[i]!="")
{
System.out.println(words[i]+"="+fr[i]);
total+=fr[i];
}
}
System.out.println("Total words counted: "+total);

 }

}

Java Program to Find Sub string Frequency in a Given String


import java.io.*;

class FrequencyCountSubString
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     
        System.out.println("Enter the String: ");
        String s=br.readLine();
        int len1 = s.length();
     
        System.out.println("Enter substring: ");
        String sub=br.readLine();
        int len2 = s.length();

        int ind,count=0;
     
        for(int i=0; i+len1<=len2; i++)  
        {
            ind=s.indexOf(sub,i);

            if(ind>=0)
            {
                count++;
               i=ind;

            }
        }
        System.out.println("Occurence of '"+sub+"' in String is "+count);

    }
}

JDBC Insertion Operation using Java & MySQL Data Base

import java.sql.*;

public class JDBCExampleInsert
{


   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
   static final String DB_URL = "jdbc:mysql://localhost/employee";


   static final String USER = "username";
   static final String PASS = "password";
  
   public static void main(String[] args)
   {
   Connection conn = null;
   Statement stmt = null;

   try
   {

      Class.forName("com.mysql.jdbc.Driver");

      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
    
       con.setAutoCommit(false);

      stmt = conn.createStatement();
     
    

      String sql = "CREATE TABLE employee" + "(eid INTEGER not NULL, " + " ename VARCHAR(255), " + 
                   " designation VARCHAR(255), " +  " PRIMARY KEY ( eid ))"; 


     stmt.addBatch(SQL);


     String sql= "INSERT INTO employee VALUES(' "+eid+" ',' "+ename+" ',' "+designation+" ') ";

     stmt.addBatch(SQL);

    
     String sql= "UPDATE  employee " + " SET designation  = " Asst Deve"  WHERE eid = "  '+eid+" ',' "+ename+" ',' "+designation+" ') ";

     stmt.addBatch(SQL);

           int[] count = stmt.executeBatch();



    }
     catch(SQLException se)
    {
      se.printStackTrace();
     con.rollback();
   }
   
     catch(Exception e)
     {
      e.printStackTrace();
    }

    finally
      {
      try
        {
         if(stmt!=null)
            stmt.close();
      }
      catch(SQLException se2)
      {
      }

      try
      {
         if(conn!=null)
            conn.close();
      }
        catch(SQLException se)
       {
         se.printStackTrace();
      }
   }
}
}

JDBC Program using Java & MySQL DB

import java.sql.*;

public class JDBCExample
{


   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
   static final String DB_URL = "jdbc:mysql://localhost/";


   static final String USER = "username";
   static final String PASS = "password";
 
   public static void main(String[] args)
   {
   Connection conn = null;
   Statement stmt = null;

   try
   {

      Class.forName("com.mysql.jdbc.Driver");

      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
   
       con.setAutoCommit(false);

      System.out.println("Creating database...");
      stmt = conn.createStatement();
     
      String sql = "CREATE DATABASE STUDENTS";
      stmt.executeUpdate(sql);
      System.out.println("Database created successfully...");

    }
     catch(SQLException se)
    {
      se.printStackTrace();
     con.rollback();
   }
   
     catch(Exception e)
     {
      e.printStackTrace();
    }

    finally
      {
      try
        {
         if(stmt!=null)
            stmt.close();
      }
      catch(SQLException se2)
      {
      }

      try
      {
         if(conn!=null)
            conn.close();
      }
        catch(SQLException se)
       {
         se.printStackTrace();
      }
   }
}
}