-
Notifications
You must be signed in to change notification settings - Fork 0
/
Frequency_with_functions.java
72 lines (65 loc) · 2 KB
/
Frequency_with_functions.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*****************************************************************************************************************************************************
* File : frequency_of_string.java
* Description : To find frequency of string using functions
* Author : Jibin Gigi
* Version : 1.0
* Date : 03/10/23
******************************************************************************************************************************************************/
package javalab;
/*import java.util.Scanner;
public class Frequency_with_functions {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String: ");
String input=sc.next();
char letter;
System.out.println("Enter the letter to check frequency: ");
letter=sc.next().charAt(0);
int count=count(count);
if(count>0) {
System.out.println("Frequency of the String is:"+count);
}
else {
System.out.println("Character is not present");
}
}
static int count(int count) {
int length=input.length(),counter=0,i=0;
char []charArray=input.toCharArray();
while(length>0) {
if(charArray[i]==letter) {
counter++;
}
i++;
length--;
}
}
}
*/
import java.util.Scanner;
public class Frequency {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a string: ");
String word = new String();
word = scan.nextLine();
word = word.toLowerCase();
char [] wordTwo = word.toCharArray();
System.out.println("Enter the letter to check frequency: ");
String letter = scan.next();
letter = letter.toLowerCase();
char [] letterTwo = letter.toCharArray();
int len = word.length();
int value = frequency(letterTwo, wordTwo,len);
System.out.println("The frequency count is "+value);
}
static int frequency(char [] letterTwo, char [] word, int len) {
int count = 0;
for (int i = 0; i < len;i ++) {
if (word[i] == letterTwo[0]) {
count ++;
}
}
return count;
}
}