how to count the occurrence of a word in string python
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count(substring)
# print count
print("The count is:", count)
how to count the occurrence of a word in string python
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count(substring)
# print count
print("The count is:", count)
how to find occurrence of words in string
HOW TO COUNT OCCURRENCE OF THE WORDS IN STRING
(It can also be solved by using nested for loops...)
String str = "I am happy and why not and why are you not happy you should be";
String [] arr = str.split(" ");
Map<String, Integer> map = new HashMap<>();
for (int i=0 ; i < arr.length ; i++){
if (!map.containsKey(arr[i])){
map.put(arr[i],1);
} else{
map.put(arr[i],map.get(arr[i])+1);
}
}
for(Map.Entry<String, Integer> each : map.entrySet()){
System.out.println(each.getKey()+" occures " + each.getValue() + " times");
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us