Pages

Find each character of a string in another string

Find each character count of a string in another string

  • Given 2 strings. Find each character count of one string in another string.
Examples:

Input: 
String 1 : keep learning
String 2  : learn
o/p:  l==1 e==3 a==1 r==1 n==2

Here, find the count of each character of string 2 in string 1

Input: 
String 1 : blogger
String 2  : blog
o/p:  b==1 l==1 o==1 g==2

Program:
public class findCharacterCount {
public static void main(String[] args) {
String str = "blogger";
String find = "blog";
int count = 0;


for (int i = 0; i <= find.length() - 1; i++) {
for (int j = 0; j <= str.length() - 1; j++) {
if (find.charAt(i) == str.charAt(j)) {
count++;
}
}
// getting each character count and print
System.out.print(find.charAt(i) + "==" + count + " ");
// assign count as 0 to get each character count
count = 0;
}

}
}
Output:
Find each character of a string in another string
Find each character of a string in another string

Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.