Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Finding the count of a letter in a String?

  1. #1
    Junior Member vlkn448's Avatar
    Join Date
    Jul 2012
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Finding the count of a letter in a String?

    Hi,
    I want to find the count of a specific letter by recursion.
    Can you help me?

    Thanks!


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Finding the count of a letter in a String?

    lets say you have a string name NAME and you wanted to count numbers of 'a' in it.
    You should try

    int count = 0; //to count the numbers of appearance of letter

    for(int i = 0; i < NAME.length(); i++) //NAME.length() is a command to count length of characters in a string.
    {
    if(NAME.charAt(i) == 'a')
    count++;
    }

    print(number of a in NAME is count)

    This should help you

  3. #3
    Junior Member vlkn448's Avatar
    Join Date
    Jul 2012
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Finding the count of a letter in a String?

    Quote Originally Posted by kindk12 View Post
    lets say you have a string name NAME and you wanted to count numbers of 'a' in it.
    You should try

    int count = 0; //to count the numbers of appearance of letter

    for(int i = 0; i < NAME.length(); i++) //NAME.length() is a command to count length of characters in a string.
    {
    if(NAME.charAt(i) == 'a')
    count++;
    }

    print(number of a in NAME is count)

    This should help you
    Thanks but I already know it.I want to do it by recursion :/

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Finding the count of a letter in a String?

    Quote Originally Posted by vlkn448 View Post
    Thanks but I already know it.I want to do it by recursion :/
    Hello vlkn448!
    Are you familiar with the concept of recursion? What have you tried so far?

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Finding the count of a letter in a String?

    vlkn448
    a piece of cake.
    ...
    you run java Recur vulkan443 4 you'll get the result 2 letter(s) 4
    Last edited by copeg; July 15th, 2012 at 09:26 AM.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Finding the count of a letter in a String?

    @voodoo, lets not do other peoples homework for them. Your code has been removed as per the forum rules. Please read:
    http://www.javaprogrammingforums.com...n-feeding.html

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Finding the count of a letter in a String?

    OKAY, sorry!

  8. #8
    Junior Member vlkn448's Avatar
    Join Date
    Jul 2012
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Finding the count of a letter in a String?

    I found! Here is my function

    	public static int finEs(String kelime){
     
    		if(kelime.equals("")){
    			return 0;
    		}else{
    			int k = 0;
    			if(kelime.substring(0, 1).equals("e")){
    				k++;
    			}
     
    			return k + eBul(kelime.substring(1));
    		}

Similar Threads

  1. Count Number of Each Letter in Given Word?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2011, 07:55 PM
  2. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  3. finding String in a file
    By nasi in forum Java Theory & Questions
    Replies: 12
    Last Post: May 31st, 2010, 01:16 PM
  4. Incrementing every letter in a string that occupies an odd position.
    By haktheplanet in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2010, 04:45 AM
  5. Sentence and Letter Count Program
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 10th, 2010, 12:10 AM