Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

11.13 Write a script that inputs several lines of text and a search character an

ID: 3764559 • Letter: 1

Question

11.13 Write a script that inputs several lines of text and a search character and uses String method indexOf to determine the number of occurrences of the character in the text.

Use this script to help:

public class AlphabetFrequencyString {

   

    static int i,j,k,c=0,w;

   

    static char m;                      //we can only define static for variables and fns not for arrays

   

    public static void main(String[] args) {

        System.out.println("Input string is : ");

        System.out.println("Alive is awesome");

        System.out.println("");

        System.out.println("");

        System.out.println("Output :");

        frequencycount("Alive is awesome");

    }

   

   

    static void frequencycount(String s)

   

    {

       

        char[] z=new char[s.length()];

        for(w=0;w<s.length();w++)

        z[w]=s.charAt(w);

        for(i=0;i<w;i++)

        {

            char ch=z[i];

            for(j=i+1;j<w;j++)

            {

                if(z[j]==ch)

                {

                    for(k=j;k<(w-1);k++)

                    z[k]=z[k+1];

                    w--;

                    j=i;

                }

            }

        }

       

        int[] t=new int[w];

        for(i=0;i<w;i++)

        {

            for(j=0,c=0;j<s.length();j++)

            {

                if(z[i]==s.charAt(j))

                c++;

            }

            t[i]=c ;

            System.out.print(z[i]+"="+c+",");

        }

    }

   

}

Explanation / Answer

import java.util.Scanner;

/**
* @author srinu
*
*/
public class AlphabetFrequencyString {

   /**
   * @param args
   */
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       try {
          

           String line = "";
           String paragraph = "";

           System.out.println("Enter the text: ");
          
           do {
               line = scanner.nextLine();
               paragraph = paragraph + line + " ";
           } while (!line.equals("exit"));
          
           System.out.print("Enter the character to search: ");

           char search = scanner.nextLine().charAt(0);

           System.out.println("Output :");
           int charOccur = frequencycount(paragraph, search);
           System.out.println("Number of times Character Occured :"
                   + charOccur);

       } catch (Exception e) {
           // TODO: handle exception
       } finally {
           scanner.close();
       }
   }

   /**
   * @param s
   * @param search
   * @return
   */
   static int frequencycount(String s, char search)

   {
       int charOccur = 0;

       for (int index = s.indexOf(search); index >= 0; index = s.indexOf(
               search, index + 1)) {
           charOccur++;
       }

       return charOccur;
   }

}

OUTPUT:

Enter the text:
Alive is awesome
exit
Enter the character to search: e
Output :
Number of times Character Occured :4