import java.util.*;
public class WordsCounter1
{
public static void main(String[] args)
{
String s;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a String : ");
s=scan.nextLine();
countWords(s);
}
public static void countWords(String st)
{
int total = 0;
String[] words=st.split(" ");
int len = words.length;
int[] fr=new int[len];
for(int i=0;i<len;i++)
{
for(int j=0;j<len;j++)
{
if(words[i].equals(words[j]))
{
fr[i]++;
}
}
}
for(int i=0;i<len;i++)
{
for(int j=0;j<len;j++)
{
if(words[i].equals(words[j]))
{
if(i!=j)
words[i]="";
}
}
}
System.out.println("Duplicate words:");
for(int i=0;i<len;i++)
{
if(words[i]!="")
{
System.out.println(words[i]+"="+fr[i]);
total+=fr[i];
}
}
System.out.println("Total words counted: "+total);
}
}
No comments:
Post a Comment