Answers for "counting occurrences of word in string in other string"

0

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)
Posted by: Guest on June-05-2020
0

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");
    }
Posted by: Guest on January-14-2021

Code answers related to "counting occurrences of word in string in other string"

Python Answers by Framework

Browse Popular Code Answers by Language