Monday, August 20, 2012

How to count duplicate words from a sentence in java



Output :

no of word {android=2, java=3, example=1}

DuplicateString.java

package com.swain.cell;

import java.util.HashMap;
import java.util.StringTokenizer;

public class DuplicateString {

       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub

        String str = "java java android android java example";
        
        StringTokenizer t = new StringTokenizer(str);
        HashMap<String,Integer> hms=new HashMap<String,Integer>();
       
              while(t.hasMoreTokens())
              {
                    
                     String word = t.nextToken();
                      hms.put(word,(hms.get(word)==null?1:hms.get(word)+1));
             
              }
              System.out.println("no of words " + hms.toString());
       
   
       }

}

How to count word in string using java



Output:
Number of words are: 6

CountWords.java

package com.swain.cell;

import java.util.StringTokenizer;

public class CountWords {

       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub
        String st="how to count word in java?";
        int count=0;
        StringTokenizer stk=new StringTokenizer(st," ");
        while(stk.hasMoreTokens()){
            String token=stk.nextToken();
            count++;
        }
        System.out.println("Number of words are: "+count);

       }

}

Friday, August 10, 2012

Palindrome


Palindrome
Output
Number is palindrome!

Source code
Palindrome.java

package com.swain.cell;

public class Palindrome {

       public static void main(String [] args){
                try{
               
                int num= 161;
                int n = num;
                int rev=0;
                for (int i=0; i<=num; i++){
                int r=num%10;
                num=num/10;
                rev=rev*10+r;
                i=0;
                }
                 if(n == rev){
                System.out.print("Number is palindrome!");
                }
                else{
                System.out.println("Number is not palindrome!");
                }
                }
                catch(Exception e){
                System.out.println("Error");
                }
                }
}


Fibonacci


Fibonacci
Output
1:- 1
2:- 1
3:- 2
4:- 3
5:- 5
6:- 8
7:- 13
8:- 21
9:- 34
10:- 55
11:- 89
12:- 144
13:- 233
14:- 377
15:- 610

Source code
package com.swain.cell;

public class Fibonacci {

        public static long fibonacci(int n) {
               if (n <= 1) return n;
               else return fibonacci(n-1) + fibonacci(n-2);
           }

           public static void main(String[] args) {
               int n=15;
               for (int i = 1; i <= n; i++)
                   System.out.println(i + ":- " + fibonacci(i));
           }

}

Linear Search


Linear Search
Output
Linear Search List
44 index  position 7

Source code
package com.swain.cell;

public class LinearSearch {
       public static void main(String[] args)
       {
              System.out.println("Linear Search List");
             
              int[] arr = {0,2879, 15, 29, 78, 26, 24, 44, 167, 13,24,47};
             
              int i;       
              int num = 44;
              boolean flag=false;
             
              for(i=0; i<arr.length; i++){
                     if(arr[i]==num){
                           flag=true;
                           break;
                     }
                    
              }//for
             
              if(flag){
                     System.out.println(num + " index  position "+i);
              }
              else{
                     System.out.println(num + " no not found");
              }
             
       }     

}

Binary Search


Binary Search
Output
---------------------------------
ENTER THE NUMBER TO BE SEARCHED
8
ENTER 10 NUMBERS FOR THE ARRAY
12
23
34
56
10
23
23
34
34
45
CONTENTS OF ARRAY ARE
12
23
34
56
10
23
23
34
34
45
NUMBER TO BE SEARCHED IS 8
NUMBER DOES NOT EXIST IN THE ARRAY

Source code
package com.swain.cell;

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

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

       int i;
       InputStreamReader x=new InputStreamReader(System.in);
       BufferedReader y=new BufferedReader(x);
       int a[]=new int[10];
       System.out.println("ENTER THE NUMBER TO BE SEARCHED");
       int n=Integer.parseInt(y.readLine());
       System.out.println("ENTER 10 NUMBERS FOR THE ARRAY");
       for(i=0;i<10;i++)
       {
       a[i]=Integer.parseInt(y.readLine());    
   }
   System.out.println("CONTENTS OF ARRAY ARE");
   for(i=0;i<10;i++)
   {
       System.out.println(a[i]);
   }
   System.out.println("NUMBER TO BE SEARCHED IS "+n);
   int p=-1,mid,l=0,u=9;
   while(l<=u)
   {
       mid=(l+u)/2;
       if(a[mid]==n)
       {
              p=mid;
              break;
       }
       else if(n> a[mid])
       {
              l=mid+1;
       }
       else if(n<a[mid])
       {
              u=mid-1;
       }
   }
   if(p==-1)
   {
       System.out.println("NUMBER DOES NOT EXIST IN THE ARRAY");
   }
   else
   {
       System.out.println("NUMBER EXISTS AT THE INDEX "+p);
   }
}
}

// amazon ebay shopbob.com hotels.com canon newegg.com 

Pyramid Program 4


Pyramid Program 4
 Output
----------
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1

Pyramid Program 4
Source code
-------------
package com.swain.cell;

public class Piramid4 {

       public static void main(String args[]){
       int i,k,j;
       for(i=1;i<=200;i= i*2){

              for(j=1; j<=i; j=j*2)
              System.out.print(j + " ");

              for(k=i/2; k>=1; k=k/2)
              System.out.print( k + " ");
              System.out.print("\n");
       }
}
}

How ChatGPT can Benefit Coding: Your Guide to Leveraging an AI Language Model

 Introduction: Hello, coders! Welcome to this blog post on how ChatGPT, an AI language model, can benefit your coding skills and projects. A...