-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFreqOfCharacter.java
More file actions
34 lines (31 loc) · 1.06 KB
/
FreqOfCharacter.java
File metadata and controls
34 lines (31 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* The FreqOfCharacter program implements an application that
* that finds the frequency of a given character in a string
*
* @author Sarju S
* @version 1.0
* @since 2020-10-01 */
package com.sjcet.basicPrograms;
import java.util.Scanner;
public class FreqOfCharacter {
public static void main(String[] args) {
String input;
int count=0;
System.out.println("Please Enter the String:");
Scanner sc = new Scanner(System.in);
input =sc.nextLine();
//Converting string to lower case
String inputCopy = input.toLowerCase();
System.out.println("Please Enter the Character to Search:");
char charToSearch = sc.next().charAt(0);//Read a character using Scanner Class
//Converting character to lower case
char charToSearchCopy = Character.toLowerCase(charToSearch);
char [] array = inputCopy.toCharArray();
for(char ch:array) {
if(ch==charToSearchCopy) {
count++;
}
}//end for
System.out.println("The frequency of a given character:"+charToSearch+" in the string: "+input+ " is: "+count);
}// end main
}