1
5 answers
0
2
/*
* Find the frequency of letters in a word
*/
import java.util.*;
class FreqLetters
{
public static void main (String args[])throws InputMismatchException
{
Scanner scan = new Scanner(System.in);
int i,j,f;
String word;
System.out.println("Enter a word: ");
word = scan.next();
char a[] = word.toCharArray();
System.out.println("Letter\t Frequency");
for(i=0; i < a.length; i++){
f = 1;
for(j=i+1; j < a.length; j++){
if(a[i] == a[j] && a[j]!=' '){
a[j]=' ';
f++;
}
}
if(a[i]!=' ')
System.out.println(" "+a[i]+ "\t" + f);
}
}//end of main
}//end of class
0
1
@Sonali - Since we are placing a blank space at the repeated letters, we do not want to compare blank spaces. The statement a[j]!=' ' will make sure that only non-blank characters are compared and counted.
0
Log in to your account to answer this questions.