http://www.technicalpage.net/search/label/SQL

CharRepetition

> Find how many times each char has repeated in a word or a sentence
> Find each unique char present in a word or a sentence

package test;

import java.util.ArrayList;

import java.util.Arrays;

 

public class CharWithRepeatdTimes {

 

       public static void main(String[] args) {

             String word = "there are 7 days in a week.";

             //word = word.trim();

             String word1 = word.replaceAll(" ","");

             //String word1 = word.replaceAll("\\s","");

             System.out.println("trimmed word " + word1); //thereare7daysinaweek.

             int wordLgth = word1.length();

             String[] wordArray = new String[word1.length()];

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

                    wordArray[i] = Character.toString(word1.charAt(i));

             }

             Arrays.sort(wordArray);

             ArrayList<String> alist1 = new ArrayList<>();//for individual elements/chars

             ArrayList<Integer> blist1 = new ArrayList<>();//for index of individual elements/char

             ArrayList<Integer> clist1 = new ArrayList<>();//for number of times of repeation of each char

             ArrayList<String> dlist1 = new ArrayList<>();//for displaying repeation of each char with number

             ArrayList<String> elist1 = new ArrayList<>();//for displaying non repeated chars

            

             //individual elements/chars

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

                    if(!(wordArray[i].equals(wordArray[i+1]))) {

                           alist1.add(wordArray[i]);

                    }

             }

             int arraySize = wordArray.length;

             alist1.add(wordArray[arraySize-1]); //adding last element

             System.out.println("individual elements/chars are " + alist1); // [., 7, a, d, e, h, i, k, n, r, s, t, w, y]

 

            

             //index of the individual elements/char

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

                    if(!(wordArray[i].equals(wordArray[i+1]))) {

                           blist1.add(i);

                    }

             }

             blist1.add(arraySize-1); //adding last index

             System.out.println("index of the individual elements/char are " + blist1); //[0, 1, 4, 5, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20]

            

             //each char repeation times

             clist1.add(blist1.get(0)+1);

             for(int i=1;i<blist1.size();i++) {

                    clist1.add(blist1.get(i)-blist1.get(i-1));

             }

             System.out.println("each char repeated this many times " + clist1); //1, 1, 3, 1, 5, 1, 1, 1, 1, 2, 1, 1, 1, 1]

            

             //for displaying repeation of each char with number

             String str = "";

             for(int i=0;i<blist1.size();i++) {

                    str = (Integer.toString(clist1.get(i))).concat(alist1.get(i));

                    dlist1.add(str);

             }

             System.out.println("each char with repeation time " + dlist1); //[1., 17, 3a, 1d, 5e, 1h, 1i, 1k, 1n, 2r, 1s, 1t, 1w, 1y]---17 means 7 is repeated 1 time, do not get confused---

            

             ////for displaying non repeated chars

             for(int i=0;i<clist1.size();i++) {

                    if(clist1.get(i)==1) {

                           elist1.add(alist1.get(i));

                    }

             }

             System.out.println("non repeated chars " + elist1); //[., 7, d, h, i, k, n, s, t, w, y]

            

            

Output:

trimmed word thereare7daysinaweek.

individual elements/chars are [., 7, a, d, e, h, i, k, n, r, s, t, w, y]

index of the individual elements/char are [0, 1, 4, 5, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20]

each char repeated this many times [1, 1, 3, 1, 5, 1, 1, 1, 1, 2, 1, 1, 1, 1]

each char with repeation time [1., 17, 3a, 1d, 5e, 1h, 1i, 1k, 1n, 2r, 1s, 1t, 1w, 1y]//---17 means 7 is repeated 1 time, do not get confused---

non repeated chars [., 7, d, h, i, k, n, s, t, w, y]

 

 

No comments:

Post a Comment