Results 1 to 13 of 13
  1. #1
    satheeshtech is offline Member
    Join Date
    Jul 2009
    Posts
    18
    Rep Power
    0

    Default How to count charectors in string?

    Hi all,
    I have a String object like String s="Hello";
    Here how to program to count occurences of each character?

    Need output like
    H - 1time,
    l -2 times ,etc..

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    In how many more forums are you going to post that same question?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    JavaJuJitZu is offline Member
    Join Date
    Jan 2010
    Posts
    26
    Rep Power
    0

    Default

    Now assume do it without the String libraries and assume the string is a c string.

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Quote Originally Posted by JavaJuJitZu View Post
    Now assume do it without the String libraries and assume the string is a c string.
    What's the use of that? It's a Java forum
    I die a little on the inside...
    Every time I get shot.

  5. #5
    satheeshtech is offline Member
    Join Date
    Jul 2009
    Posts
    18
    Rep Power
    0

    Default

    hi satheesh.,
    i could not get your code yar... can you explain it pls

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by satheeshtech View Post
    hi satheesh.,
    i could not get your code yar... can you explain it pls
    Why did you abandon your other thread?

    How to count occurences of charectors in string?

    Was it because you were asked to write the code for a change?

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Quote Originally Posted by r035198x View Post
    Why did you abandon your other thread?

    How to count occurences of charectors in string?

    Was it because you were asked to write the code for a change?
    You didn't spoon feed him. In this thread he at least got useless code.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    I'm pretty sure I read a java tutorial on this on Sun's website. If you are looking for code, it might be there.
    Nvm, found it.
    Java Code:
    import java.util.*;
    
    public class Freq {
        public static void main(String[] args) {
            Map<String, Integer> m = new HashMap<String, Integer>();
    
            // Initialize frequency table from command line
            for (String a : args) {
                Integer freq = m.get(a);
                m.put(a, (freq == null) ? 1 : freq + 1);
            }
    
            System.out.println(m.size() + " distinct words:");
            System.out.println(m);
        }
    }
    From The Map Interface (The Java™ Tutorials > Collections > Interfaces)

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by collin389 View Post
    I'm pretty sure I read a java tutorial on this on Sun's website. If you are looking for code, it might be there.
    Nvm, found it.
    ...
    Nothing the OP doesn't have to do anything.
    Was better to let the OP learn better by trying it themselves first.

  10. #10
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    I think it's was pretty obvious that the OP doesn't want to learn and just wants the code(since he started 2 threads asking for "just code"). Besides, hopefully there are parts he doesn't understand and will look those parts up.

  11. #11
    satheeshtech is offline Member
    Join Date
    Jul 2009
    Posts
    18
    Rep Power
    0

    Default

    got the answer. thank u all

    String text="satheesh";
    text=text.toLowerCase();
    int charNum=26; // the number of unique characters

    int[] freq=new int[charNum];


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

    freq[text.charAt(i)-'a']++;
    System.out.println(text.charAt(i)-'a');
    }

    for(int i=0;i<charNum;i++) {
    System.out.println("Frequency of " + (char) (97 + i) +": " + freq[i]);
    }

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Or possibly he won't, will still get whatever worthless piece of paper as a qualification, write some utter rubbish in the real world, and I'll have to clean it up...

    If he doesn't want to learn then don't give him the opportunity to pass!

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Or possibly he won't, will still get whatever worthless piece of paper as a qualification, write some utter rubbish in the real world, and I'll have to clean it up...
    That's one of the reasons it's still such a mess in the ICT world: too many no-goods bluff their way into it, big mouth their way into some stolen/borrowed/copied code and turn everything into one big mess and others have to clean up after those people have gone again.

    kind regards,

    Jos

Similar Threads

  1. to get count value as a variable
    By arunkumarinfo in forum JDBC
    Replies: 2
    Last Post: 03-30-2009, 02:32 AM
  2. post count
    By codeflip in forum Forum Lobby
    Replies: 2
    Last Post: 03-12-2009, 03:20 AM
  3. Replies: 8
    Last Post: 02-04-2009, 09:55 PM
  4. Replies: 2
    Last Post: 05-04-2008, 11:52 PM
  5. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 09:49 AM

Posting Permissions

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