#2: How to count character Occurrence in a given String | Java interview Program for testers

preview_player
Показать описание
Please share, support and subscribe.
Follows us on:

Automation Framework Questions

Selenium Interview Questions

About Online Rostrum:

This is a tech channel and we are sharing knowledge and helping our community.
Рекомендации по теме
Комментарии
Автор

public class A {

public static void main(String[] args) {
String str="Java is object oriented language";
int count=0;
for (int i = 0; i < str.length()-1; i++) {

if (str.charAt(i)=='a') {

}
}
System.out.print(count);
}
}

PrasadPoojar-mk
Автор

Get count for specific character and for all characters need only to remove filter condition:
List<String> characters = Arrays.asList(str.split(""));
Map<String, Long> map = characters.stream()
.filter(s -> s.equalsIgnoreCase("a"))
-> i, Collectors.counting()));

olenakhomenko
Автор

The simple way is :
String str = "character Occurrence in a given String";

long result =

System.out.println(result);

nikhilmucharla
Автор

We need to use the toLowerCase since if we can search with 'j' it will discard the J in "Java" Word, giving the wrong count in this case

public class Occurrence {
public static void main(String[] args) {
String str="Java is Object oriented language";
char neededLetter= 'j';
int count=0;
for(char
if(letter==neededLetter)
count ++;
}
System.out.println("The character "+neededLetter+" is having: "+count);

}

christenajacoub
Автор

how to count all the character count? i mean all the letters with its count ?

sunil_gowda
join shbcf.ru