Skip to content

Commit fcd7ee9

Browse files
Create 38.1 count letter in string.java
1 parent 91f9d4e commit fcd7ee9

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

38.1 count letter in string.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Write a program to take a String input from the user having length greater than 3. Ask the user to give character input and find how many no. of times that character is present in the string. If length of the string entered by the user is <=3, then display message "Invalid".
3+
4+
Input Format
5+
6+
Program should take 2 inputs in following sequence: 1) String input 2) Character input
7+
8+
Constraints
9+
10+
If length of the string entered by the user is <=3, then display message "Invalid" and the second input (i.e. character input) should not be taken from user.
11+
12+
Output Format
13+
14+
Display how many no. of times a particular character (specified by user at run-time) is present in the string.
15+
16+
Sample Input 0
17+
18+
brilliant
19+
l
20+
Sample Output 0
21+
22+
2
23+
*/
24+
import java.io.*;
25+
import java.util.*;
26+
27+
public class Solution {
28+
29+
public static void main(String[] args) {
30+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
31+
Scanner sc = new Scanner(System.in);
32+
String str = sc.next();
33+
if(str.length()>3)
34+
{
35+
char []input = str.toCharArray();
36+
char c = sc.next().charAt(0);
37+
int count = 0;
38+
for(char s : input)
39+
{
40+
if(s==c)
41+
count++;
42+
}
43+
System.out.print(count);
44+
}
45+
else
46+
System.out.print("Invalid");
47+
48+
49+
}
50+
}

0 commit comments

Comments
 (0)