Post Reply  
 
Thread Tools Search this Thread
07-02-2010, 16:22   #1
 
Join Date: Jan 2010
Posts: 400
Java program to count occurence of letters

Hi this is my first time posting in this forum so forgive any mistakes! Okay so I'm learning Java in college and I need help with a program. The idea for the program is for the user to input a sentence, and the program will count how many times each letter is in the string, ie. "Abba" will give the results:
A=2
B=2

I've made the input string into an array and my method is to search the array for letters. Here is the code so far:



public class SentenceProgram {
public static void main (String args[]) {

Terminal terminal;
String Sentence;
char [] characterArray;

terminal = new Terminal("Sentence Program");
String stringSentence = terminal.readString("Enter in sentence: ");
terminal.println("");


char[] chars = stringSentence.toCharArray();

/*count how many times 'A' is in the sentence*/
int countA = 0;
for(int i = 0; i < chars.length; i++) {
if(chars[i] == 'A')
countA++;
if (chars [i] == 'a')
countA++;
}
if (countA != 0)
terminal.println ("Letter 'A' occurs: " + countA + " times");


}
}



My problem is I'm not sure how to make a method to check every letter. In my code I have a method to check for upper and lower case a but I don't want to have to write this 26 times for every letter. Does anyone know how to write a method to search the array for every letter? Thanks.
TheRiddler is offline  
Advertisement
07-02-2010, 16:36   #2
 
Join Date: Sep 2009
Posts: 380
string = string.trim();
removes trailing leading white spaces, improves performance.

string = string.toLowerCase();
Now you don't have to check A and a

Have an array of the alphabet and loop through it to test.
letters[]

Have an array of ints to represent the occurances. Set all to 0 first.
occur[]


Then the statement to print the results is
String space = " = ";
for(int i=0;i<26;i++){
terminal.println(letters[i]+space+occur[i]);
}
kenbrady is offline  
Thanks from:
07-02-2010, 16:52   #3
 
Join Date: Jan 2010
Posts: 400
Great got it working thanks! Mods you can close this now
TheRiddler is offline  
07-02-2010, 21:40   #4
Evil Phil
Contact Section
 
Evil Phil's Avatar
 
Join Date: Mar 2001
Posts: 7,284
Nah we'll leave it open as somebody else may have input or a question. I'll change it to [Solved] though.
Evil Phil is offline  
17-02-2010, 15:15   #5
GreeBo
Registered User
 
GreeBo's Avatar
 
Join Date: Oct 2003
Posts: 23,371
You could also do something like

Stringp[] alphas = new String {"a","b",...."z"};
myString = "abcdshkjbdsd";

len = myString.length();

for (string char : alphas)
myString = myString.replace(char, "");
newLen = myString.length();
print("num "+char+": "+len - newLen);
len = newLen;
done

One benefit is that the string you are checking gets shorter and shorter each time.

Last edited by GreeBo; 17-02-2010 at 15:22.
GreeBo is offline  
Advertisement
17-02-2010, 15:47   #6
km991148
Registered User
 
Join Date: Nov 2007
Posts: 2,479
Or sort it alphabetically (after doing tolower on it) then work through it as follows:

for curr=0; curr<totallength; curr++{
{
while(curr!= curr+1)
{count it}
display letter and count
update curr
}


Sorry I was bored - one of these ones with a hundred ways of implementing..
Not even sure if there is any benefit to the above due to the sort..
km991148 is offline  
17-02-2010, 15:49   #7
GreeBo
Registered User
 
GreeBo's Avatar
 
Join Date: Oct 2003
Posts: 23,371
Quote:
Originally Posted by km991148 View Post
Sorry I was bored - one of these ones with a hundred ways of implementing..
Not even sure if there is any benefit to the above due to the sort..
Is there a better reason to be on here?

I think the sort would kill you there though...
GreeBo is offline  
17-02-2010, 16:12   #8
km991148
Registered User
 
Join Date: Nov 2007
Posts: 2,479
Quote:
Originally Posted by GreeBo View Post
Is there a better reason to be on here?

I think the sort would kill you there though...
ha,

ye to both..

But then Sorting is a whole other exercise for the OP..
km991148 is offline  
17-02-2010, 16:35   #9
ronivek
Registered User
 
Join Date: Mar 2007
Posts: 1,335
An ideal procrastination opportunity.

Code:
public static void printAlphabetCharOccurrences(String str)
{
    int modifier = 97;
    int[] charCount = new int[('z' - 'a') + 1];
    String lowerCaseStr = str.toLowerCase();
    char c;
    for (int i = 0; i < lowerCaseStr.length(); i++) {
        if (Character.isLetter(c = lowerCaseStr.charAt(i))) {
            charCount[c - modifier]++;
        }
    }

    System.out.println("Occurrences of each letter in string: " + str);
    for (int i = 0; i < charCount.length; i++)
        System.out.println((char)(i + modifier) + ": " + charCount[i]);
}
ronivek is offline  
Post Reply

Quick Reply
Message:
Remove Text Formatting
Bold
Italic
Underline

Insert Image
Wrap [QUOTE] tags around selected text
 
Decrease Size
Increase Size
Please sign up or log in to join the discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Share Tweet