Showing posts with label Program. Show all posts
Showing posts with label Program. Show all posts

Monday, August 20, 2012

how to count selected Character from a sentence in java


Output:

C contents  3 times.

DuplicateCharacter.java

package com.swain.cell;

import java.util.HashMap;

public class DuplicateCharacter {

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

        String str = "Character duplicate word example java";
        char selectedchar='C';
        char ch1[]=str.toUpperCase().toCharArray();
        HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
        for(int i=0; i<ch1.length; i++)
        {
            char ch=ch1[i];
            hm.put(ch,(hm.get(ch)==null?1:hm.get(ch)+1));
        }
      
        System.out.println(selectedchar+" contents  " + hm.get(selectedchar)+ " times.");
       
       
   
       }

}


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

how to count selected word in string using java


Output:

java contents  3 times.

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";
        String selectedword="java";
        
        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(selectedword+" contents  " + hms.get(selectedword)+ " times.");
       
   
       }

}


Check here video.




How to count duplicate characters from a sentence in java



Output :

DuplicateCharacter.java

package com.swain.cell;

import java.util.HashMap;

public class DuplicateCharacter {

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

        String str = "Character duplicate word example java";
        char ch1[]=str.toCharArray();
        HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
        for(int i=0; i<ch1.length; i++)
        {
            char ch=ch1[i];
            hm.put(ch,(hm.get(ch)==null?1:hm.get(ch)+1));
        }
      
        System.out.println("No of Characters are : "+hm.toString());
       
       
   
       }

}

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");
                }
                }
}


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 

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...