Counting Letters using Arrays
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Counting Letters using Arrays

  1. #1
    Join Date
    Feb 2011
    Posts
    5

    Counting Letters using Arrays

    This is Homework so writing out the answer would be unacceptable.

    Anyways, The objective is to write a method that will pass a set of test in which it counts the Letters in the test. I have little to no idea what to do and what I got is from messy notes. My professor says that we can us a "helper method" called isLetter. However I do not know how I would define this method in which it would "check" to see if the statements in the test have a certain letter. The reason the size of the array is 27 is because not only does it include the alphabet, but it also include a symbol. Thanks ahead of time.

    public int[] letterCounts(String s) {
    int[] counts = new int[27];

    for (int i= 0; i < s.length(); ){

    if(isLetter(s.charAt(i))){
    counts[i - 'a']++;

    }
    else{
    }
    counts[26]++;
    }
    return counts;
    }
    private boolean isLetter(char c){
    return true;
    }
    }

  2. #2
    Join Date
    Feb 2011
    Posts
    27

    Re: Counting Letters using Arrays

    If you cast a character as an integer, that character's ascii value will be returned. Uppercase letters have ascii values between 65 and 90 and lowercase letters have ascii values between 97 and 122.

    A more complete ascii table can be found here: http://www.asciitable.com/

    You may be able to use this in your helper isLetter() method.

    Also there are a couple things about the code you posted:

    Code:
    for (int i= 0; i < s.length(); ){
    
       if(isLetter(s.charAt(i))){
          counts[i - 'a']++;  /* the 'i' here refers to an index into the String, not the character itself */
       }
       else{
       } /* your braces seem to mess up starting here */
       counts[26]++;
    }
    return counts;
    }
    Last edited by bgroves85; February 8th, 2011 at 08:25 PM.

  3. #3
    Join Date
    Feb 2011
    Posts
    5

    Re: Counting Letters using Arrays

    Quote Originally Posted by bgroves85 View Post
    If you cast a character as an integer, that character's ascii value will be returned. Uppercase letters have ascii values between 65 and 90 and lowercase letters have ascii values between 97 and 122.

    A more complete ascii table can be found here: http://www.asciitable.com/

    You may be able to use this in your helper isLetter() method.

    Also there are a couple things about the code you posted:

    Code:
    for (int i= 0; i < s.length(); ){
    
       if(isLetter(s.charAt(i))){
          counts[i - 'a']++;  /* the 'i' here refers to an index into the String, not the character itself */
       }
       else{
       } /* your braces seem to mess up starting here */
       counts[26]++;
    }
    return counts;
    }

    So would I switch the i to be a char value?

  4. #4
    Join Date
    Feb 2011
    Posts
    27

    Re: Counting Letters using Arrays

    Ok so your integer array called counts is going to count how many of each character there are. Every time you see an 'a' you want to increment the value stored in count[0], when you see a 'b' you increment counts[1], and so on.

    Code:
    String str = "hello";
    counts[str.charAt(1) - 'a'] ++;
    In that code example, str.charAt(1) will return 'e' because there is an 'e' at index 1 of "hello".

    When you use the '-' operator with characters, the characters are converted to their ASCII values. The ASCII value for 'e' is 101 and the ASCII value for 'a' is 97. 101 - 97 = 4. So by doing this you have found the index you need into your counts array.

    You had the right idea, but where you used just the 'i' you should have been using the ith index into the string instead.

    The reason you would want to make a helper method to determine if a give character is a letter or not is that if you use this approach and the character isn't a letter, you are going to end up with negative indexes into your counts array or indexes greater than 26.

    Also you might want to figure out whether a given letter is uppercase or lowercase, because subtracting 'a' from an uppercase letter will not give you what you are looking for.

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Counting Letters using Arrays

    Be aware that Java uses Unicode, of which ASCII is an incidental subset. In general, you should not rely on the letters having sequential numeric values - it may be true for US English, but is not necessarily true for other languages. Basically, it is not Good Practice to do math on characters assuming they happen to be in the ASCII range.

    If you use the java.lang.Character class, you'll see it provides an isLetter() method that you can pass a char to:
    Code:
    ...
    if (Character.isLetter(str.charAt(i))) {
     ...
    }
    YMMV.

    The key to performance is elegance, not batallions of special cases...
    J. Bently & D. McIlroy
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center