Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Count words in a Sentence or Count characters in a given word or string

 

package testing;

 

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

 

public class CountWordsCharsUsingHashmap {

 

     public static void main(String[] args) {

         wordCountInSentence();

         countCharsInGivenWord();

     }

        

     //word count in a sentence using Hashmap

         public static void wordCountInSentence() {

        

         String word = "this is a beautiful a this a is yes beautiful";

         Map<String, Integer> map = new HashMap<String, Integer>();

        

         String[] str = word.split(" ");

         int count = 0;

         for(int i=0;i<str.length;i++) {

              String wordAti = str[i];

              if((map.containsKey(wordAti))  ) {

                  map.put(wordAti, (map.get(wordAti))+1);

              }else {

                  map.put(wordAti, 1);

              }

         }

         System.out.println("count of words : "+map);

     }

 //------------------------------------------------------------

     //char count in a word/string using Hashmap

     public static void countCharsInGivenWord() {   

     String word1 = "asdfdefaefdea";

    

     Map<String, Integer> map = new HashMap<>();

     int len = word1.length();

     int mapKeyCount=0 ;

     for(int i=0;i<len;i++) {

         String charAti = Character.toString(word1.charAt(i));

         if(map.containsKey(charAti)) {

              mapKeyCount =map.get(charAti)+1;

         } else {

              mapKeyCount =1;

         }

         map.put(charAti, mapKeyCount);

     }

     System.out.println("count of characters: "+map);

     }

}

 

Output:

count of words : {a=3, beautiful=2, yes=1, this=2, is=2}

count of characters: {a=3, s=1, d=3, e=3, f=3}

Rotate Number or String, one char or one digit at a time.

 Rotate given string or number in Java:

Solution 1 of 2:

import java.util.Arrays;

 

public class Test {

 

     public static void main(String[] args) {

 

         String word = "beautiful";

         int len = word.length();

         String[] str = new String[len];

         String x ;

         for(int i=0;i<len; i++) {

              str[i] = Character.toString(word.charAt(i));

         }

         System.out.println("The array values : "+Arrays.toString(str));

         for(int j=0;j<len; j++) {

              x=str[0];

         for(int i=0;i<len-1; i++) {

             

                  str[i]=str[i+1];

         }

         str[len-1]=x;

                  System.out.println("The array values : "+Arrays.toString(str));

         }       

     }

}

 

Output:

The array values : [b, e, a, u, t, i, f, u, l]

The array values : [e, a, u, t, i, f, u, l, b]

The array values : [a, u, t, i, f, u, l, b, e]

The array values : [u, t, i, f, u, l, b, e, a]

The array values : [t, i, f, u, l, b, e, a, u]

The array values : [i, f, u, l, b, e, a, u, t]

The array values : [f, u, l, b, e, a, u, t, i]

The array values : [u, l, b, e, a, u, t, i, f]

The array values : [l, b, e, a, u, t, i, f, u]

The array values : [b, e, a, u, t, i, f, u, l]

 

Solution 2 of 2 :

Above Solution covers every rotating string or number of one char or one digit one time.

Below solution is specific to the problem as shown below, and it used replace in ArrayList as well as removeAll in ArrayList.

import java.util.ArrayList;

public class Test {

     public static void main(String[] args) {

         /*

         1000

         0100

         0010

         0001

         */

         ArrayList<Integer> alist = new ArrayList<>();

         ArrayList<Integer> blist = new ArrayList<>();

         int count = 0 ;

         for(int i=0;i<4;i++) {

              int a=0,b=0,c=0,d=0;

              alist.add(a);

              alist.add(b);

              alist.add(c);

              alist.add(d);

              blist = alist ;

              alist.set(count,1);//Replace ArrayList element, you should give index value of arraylist here

              System.out.println("The Arraylist values : "+alist);

              alist.removeAll(blist);

                       count ++ ;

         }    

     }

}

Output:

The Arraylist values : [1, 0, 0, 0]
The Arraylist values : [0, 1, 0, 0]
The Arraylist values : [0, 0, 1, 0]
The Arraylist values : [0, 0, 0, 1]