This code is used to count the number of occurrences of each word in a given string.
Note that this code is case sensitive, so it will count "String" and "string" as two different words.
public class CountWords { public static void main(String[] args) { String input="String and String function"; String[] words=input.split(" "); int wrc=1; for(int i=0;i<words.length;i++) { for(int j=i+1;j<words.length;j++) { if(words[i].equals(words[j])) { wrc=wrc+1; words[j]="0"; } } if(words[i]!="0") System.out.println(words[i]+" = "+wrc); wrc=1; } } }
String = 2 and = 1 function = 1
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions