Java Write a program that analyzes text written in the conso
Java!!
Write a program that analyzes text written in the console by counting the number of times each of the 26 letters in the alphabet occurs.
Uppercase and lowercase letters should be counted together (for example, both ‘A’ and ‘a’ should count as an A).
Any characters that are not letters should be ignored.
You must prompt the user to enter the text to be analyzed. Then, for any letter that appeared at least once in the text, print out the number of times it appeared (and do so in alphabetical order).
An effective way to count characters is to read from the console string-by-string, and loop through all of the characters of each of these strings.
the hasNext() method of the scanner will be useful
when testing in Eclipse, press enter and then, once on the empty line, press CTRL-D (Mac) or CTRL+Z (Windows) when you are done typing the text. You must use an array to keep track of how many times each letter is seen in the text.
The array should have 26 elements (one for each letter in the alphabet). Index 0 should be used to track the number of A’s, index 1 to track the B’s, index 2 to track the C’s, etc., up to index 25 for the Z’s.
think about how to convert each character you read into the correct index and then increment that value in the array. For example, if you read an A, then you should increment the value in index 0.
Specifically, you will need to determine if the character is an uppercase letter (between ‘A’ and ‘Z’), a lowercase letter (between ‘a’ and ‘z’), or something else. If it is a letter, convert it into the appropriate index.
Recall that characters and integers are interchangeable via the ASCII table conversion
Solution
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
public class Saplha {
public static void main(String argv[])
{
String c;
System.out.print(\"Enter string:\");
Scanner sc = new Scanner(System.in);//taking input...
c =sc.nextLine();
c.toLowerCase();
int a[]=new int[26],i;
for(i=0;i<c.length();i++)//calculating letter frequency....
{
a[(int)c.charAt(i)-97] = a[(int)c.charAt(i)-97]+1;
}
System.out.println(\"The frequency of alphabets:\");
for(i=0;i<26;i++)//printing outputs..
{
System.out.println((char)(97+i)+\":\"+a[i]);
}
}
}
output:-
run:
Enter string:adfladbfldsafk
The frequency of alphabets:
a:3
b:1
c:0
d:3
e:0
f:3
g:0
h:0
i:0
j:0
k:1
l:2
m:0
n:0
o:0
p:0
q:0
r:0
s:1
t:0
u:0
v:0
w:0
x:0
y:0
z:0
BUILD SUCCESSFUL (total time: 5 seconds)

