Page 364 - Computer science 868 Class 12
P. 364

Program 3      Write a program in Java to accept a string. Count and display the frequency of each character
                               present in the string. The character with multiple frequencies should be displayed only once.
                               Sample Input:
                               India is great

                               Sample Output:
                               Alphabet                 I  n d i  a  s g r e  t
                               Frequency                1 1 1 2 2 1 1 1 1 1

                 1       import java.util.*;
                 2       class charfreq

                 3       {
                 4           public static void main()
                 5           {

                 6               Scanner sc = new Scanner(System.in);

                 7               String str;
                 8               int len,index=0,i,j;
                 9               char ch;

                10               boolean seen;
                11               System.out.println("Enter a string:");

                12               str = sc.nextLine();
                13               len = str.length();

                14               char arr1[] = new char[len];
                15               int frarr[] = new int[len];

                16               for (i = 0; i < len; i++)
                17               {

                18                   ch = str.charAt(i);
                19                   if (Character.isWhitespace(ch))

                20                   {
                21                       continue;

                22                   }
                23                   seen = false;

                24                   for (j = 0; j < index; j++)
                25                   {

                26                       if (ch == arr1[j])
                27                       {

                28                           seen = true;



                362362  Touchpad Computer Science-XII
   359   360   361   362   363   364   365   366   367   368   369