-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequencyStrings.java
More file actions
44 lines (40 loc) · 1.08 KB
/
Copy pathfrequencyStrings.java
File metadata and controls
44 lines (40 loc) · 1.08 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
35
36
37
38
39
40
41
42
43
44
/**********************************************************************
* File :frequencyStrings.java
* Description :To find the frequency of a character
* Author :Shraya S Santhosh
* Version :1.0
* Date :3/10/2023
*******************************************************************/
package newproj;
import java.util.Scanner;
public class frequencyStrings {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String string=sc.nextLine();
System.out.println("Enter the character to check");
char character=sc.next().charAt(0);
int frequency=check(string,character);
if (frequency==0)
{
System.out.println("The character is not present in the string");
}
else
{
System.out.println("Frequency of the character is "+frequency);
}
}
static int check(String string,char character){
char []charArray=string.toCharArray();
int len=string.length(),count=0;
for(int i=0;i<len;i++)
{
if(charArray[i]==character)
{
count++;
}
}
return count;
}
}