Finding the count of a letter in a String?

• July 14th, 2012, 02:37 PM
vlkn448
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!
• July 14th, 2012, 05:17 PM
kindk12
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)

• July 14th, 2012, 05:20 PM
vlkn448
Re: Finding the count of a letter in a String?
Quote:

Originally Posted by kindk12
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)

Thanks but I already know it.I want to do it by recursion :/
• July 15th, 2012, 04:56 AM
andreas90
Re: Finding the count of a letter in a String?
Quote:

Originally Posted by vlkn448
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?
• July 15th, 2012, 06:36 AM
Voodoo
Re: Finding the count of a letter in a String?
vlkn448
a piece of cake.
Code java:

`...`
you run java Recur vulkan443 4 you'll get the result 2 letter(s) 4
• July 15th, 2012, 09:27 AM
copeg
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
• July 15th, 2012, 10:55 AM
Voodoo
Re: Finding the count of a letter in a String?
OKAY, sorry!
• July 15th, 2012, 11:55 AM
vlkn448
Re: Finding the count of a letter in a String?
I found! Here is my function

Code :

``` 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)); }```