Chrome File Edit View History Bookmarks People Window Help 8

Chrome File Edit View History Bookmarks People Window Help 8 20% D Wed Nov 9 11 30 52 AM aR E Lab Assignments Mod 6-Ch6 Programming Principles l Section 16 Fall Semester 2016 CO kennesaw.view.usg.edu 103674/fullscreen/18397105/View Program 5 Design and implement a Java program for programming exercise 6.23, page 238 (name it occurrences), to find the number of occurrences of a letter in a given string. Follow the instructions in the problem statement. Design the main method of your program such that it allows the user to re-run the program with different input strings (i.e., use a loop structure). Document your code and organize the outputs properly using escape characters View as Tex

Solution

Occurances.java


public class Occurances {

  
   public static void main(String[] args) {
       java.util.Scanner scan = new java.util.Scanner(System.in);
       System.out.println(\"Enter the string: \");
       String s = scan.nextLine();
       System.out.println(\"Enter the character: \");
       char ch = scan.next().charAt(0);
       int n = count(s,ch);
       System.out.println(\"Number of occurances of a character \"+ch+\" is : \"+n);
   }
   public static int count(String str,char a){
       int count = 0;
       for(int i=0; i<str.length();i++){
           if(str.charAt(i) == a){
               count++;
           }
       }
       return count;
   }

}

Output:

Enter the string:
welcome
Enter the character:
e
Number of occurances of a character e is : 2

 Chrome File Edit View History Bookmarks People Window Help 8 20% D Wed Nov 9 11 30 52 AM aR E Lab Assignments Mod 6-Ch6 Programming Principles l Section 16 Fal

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site