Write a programme to count the disclose of occurrences of a grapheme inwards String is 1 of the mutual programming interview questions non merely inwards Java but besides inwards other programming languages similar C or C++. As String inwards a rattling pop theme on programming interviews in addition to at that spot are lot of skilful programming practise on String similar "count disclose of vowels or consonants inwards String", "count disclose of characters inwards String" , How to opposite String inwards Java using recursion or without using StringBuffer etc, it becomes extremely of import to get got corporation cognition of String in Java or whatever other programming language. Though, this inquiry is by in addition to large used to examination candidate's coding might i.e. whether he tin give the axe convert logic to code or not.
In Interview, virtually of the fourth dimension Interviewer volition inquire yous to write a programme without using whatever API method, as Java is rattling rich in addition to it e'er or in addition to thence sort of prissy method to create the job, But it besides of import to know rich Java in addition to opened upwards rootage libraries for writing production character code.
Anyway, inwards this question, nosotros volition encounter both API based in addition to non-API based(except few) ways to count a disclose of occurrences of a grapheme inwards String on Java.
In Interview, virtually of the fourth dimension Interviewer volition inquire yous to write a programme without using whatever API method, as Java is rattling rich in addition to it e'er or in addition to thence sort of prissy method to create the job, But it besides of import to know rich Java in addition to opened upwards rootage libraries for writing production character code.
Anyway, inwards this question, nosotros volition encounter both API based in addition to non-API based(except few) ways to count a disclose of occurrences of a grapheme inwards String on Java.
Java programme to count occurrences of a grapheme inwards String
After that, nosotros volition encounter Apache common StringUtils shape for counting occurrence of a grapheme inwards String. Apache common StringUtils furnish countMatches() method which tin give the axe move used to count the occurrence of 1 grapheme or substring.
Finally, nosotros volition encounter the virtually elementary agency of counting grapheme using measure for loop in addition to Java five enhanced for loop. This solution tin give the axe move extended non merely to finding the occurrence of grapheme but besides finding occurrences of a substring.
Btw, if yous are solving this inquiry every bit component of your Java interview preparation, yous tin give the axe besides banking concern gibe Cracking the Coding Interview, a collection of 189 programming questions in addition to solutions from diverse programming undertaking interviews. Your perfect companion for developing coding feel required solving these kinds of problems on interviews.
Now, let's encounter the Java programme to count disclose of occurrence of whatever grapheme on String:
import org.springframework.util.StringUtils;
/**
* Java programme to count the disclose of occurrence of whatever grapheme on String.
* @author Javin Paul
*/
public class CountCharacters {
public static void main(String args[]) {
String input = "Today is Monday"; //count disclose of "a" on this String.
//Using Spring framework StringUtils shape for finding occurrence of or in addition to thence other String
int count = StringUtils.countOccurrencesOf(input, "a");
System.out.println("count of occurrence of grapheme 'a' on String: " +
* Java programme to count the disclose of occurrence of whatever grapheme on String.
* @author Javin Paul
*/
public class CountCharacters {
public static void main(String args[]) {
String input = "Today is Monday"; //count disclose of "a" on this String.
//Using Spring framework StringUtils shape for finding occurrence of or in addition to thence other String
int count = StringUtils.countOccurrencesOf(input, "a");
System.out.println("count of occurrence of grapheme 'a' on String: " +
" Today is Monday' using Spring StringUtils " + count);
//Using Apache common lang StringUtils class
int disclose = org.apache.commons.lang.StringUtils.countMatches(input, "a");
System.out.println("count of grapheme 'a' on String: 'Today is Monday' using common StringUtils " + number);
//counting occurrence of grapheme amongst loop
int charCount = 0;
for(int i =0 ; i<input.length(); i++){
if(input.charAt(i) == 'a'){
charCount++;
}
}
System.out.println("count of grapheme 'a' on String: 'Today is Monday' using for loop " + charCount);
//a to a greater extent than elegant agency of counting occurrence of grapheme inwards String using foreach loop
charCount = 0; //resetting grapheme count
for(char ch: input.toCharArray()){
if(ch == 'a'){
charCount++;
}
}
System.out.println("count of grapheme 'a' on String: 'Today is Monday' using for each loop " + charCount);
}
}
Output
count of occurrence of grapheme 'a' on String: 'Today is Monday' using Spring StringUtils 2
count of grapheme 'a' on String: 'Today is Monday' using common StringUtils 2
count of grapheme 'a' on String: 'Today is Monday' using for loop 2
count of grapheme 'a' on String: 'Today is Monday' using for each loop 2
//Using Apache common lang StringUtils class
int disclose = org.apache.commons.lang.StringUtils.countMatches(input, "a");
System.out.println("count of grapheme 'a' on String: 'Today is Monday' using common StringUtils " + number);
//counting occurrence of grapheme amongst loop
int charCount = 0;
for(int i =0 ; i<input.length(); i++){
if(input.charAt(i) == 'a'){
charCount++;
}
}
System.out.println("count of grapheme 'a' on String: 'Today is Monday' using for loop " + charCount);
//a to a greater extent than elegant agency of counting occurrence of grapheme inwards String using foreach loop
charCount = 0; //resetting grapheme count
for(char ch: input.toCharArray()){
if(ch == 'a'){
charCount++;
}
}
System.out.println("count of grapheme 'a' on String: 'Today is Monday' using for each loop " + charCount);
}
}
Output
count of occurrence of grapheme 'a' on String: 'Today is Monday' using Spring StringUtils 2
count of grapheme 'a' on String: 'Today is Monday' using common StringUtils 2
count of grapheme 'a' on String: 'Today is Monday' using for loop 2
count of grapheme 'a' on String: 'Today is Monday' using for each loop 2
Well, the beauty of this questions is that Interviewer tin give the axe twist it on many ways, they tin give the axe inquire yous to write a recursive exercise to count occurrences of a item character or they tin give the axe fifty-fifty inquire to count how many times each grapheme has appeared.
So if a String contains multiple characters in addition to yous demand to shop count of each character, catch using HashMap for storing grapheme every bit primal in addition to disclose of occurrence every bit value. Though at that spot are other ways of doing it every bit good but I similar the HashMap agency of counting grapheme for simplicity.
So if a String contains multiple characters in addition to yous demand to shop count of each character, catch using HashMap for storing grapheme every bit primal in addition to disclose of occurrence every bit value. Though at that spot are other ways of doing it every bit good but I similar the HashMap agency of counting grapheme for simplicity.
Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures in addition to Algorithms: Deep Dive Using Java
Write a programme to abide by factorial of disclose using recursion