## Need help: Using Recursion function to count a letter

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

### Need help: Using Recursion function to count a letter

My project spec is i need to count a certain letter. for example the word "alabama" i want to count how many "a" are on that word by just using a recursion function. I dont know how to start this...can someone help?
jinx

Sure. What does your code look like so far?
Need information on a function I've posted? Chances are it's at the MSDN.
MXP

Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

As an exception, I will offer some help, even without any proof of effort by the original poster.

number of `a's in "alabama" = 1 + number of `a's in "labama"
number of `a's in "labama" = 0 + number of `a's in "abama"
...

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

This is my code so far, sorry for the delay been busy....Still cant figure it out =(

Code: Select all
`#include<iostream.h>#include<string.h>int word(char);void main(){   char w[30];   cout << "Please enter a word: ";   cin  >> w;    cout << word(w);}int word(char x){   if( x[0] != 'a')   {      return ?;   }   else   {      return ?;   }   }`
jinx

Try describing what you need to do in more detail.

What are you starting with? What do you need to do with it?
Write it down step by step.
There are only 10 types of people in the world -- those that use binary, and those that don't
WaltP

Posts: 1187
Joined: Thu Oct 16, 2003 11:24 am

what i need to do is to check the letters of the array. To see if its a "a" or not.

1.i need to check the first letter of the array if its "a" if not move on to the second letter and so on.

2.my output should say how many "a" i have on the word.
jinx

Explain what you want to put instead of `?' in your code. And the function should take a pointer to char, not a char.

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

I am guessing this is what you want, not really sure but here you go.

Code: Select all
`int number_of_a_s(char value[20]);int main() {char input[20];cout<<"Please enter a word\n";cin>>input;cout<<"\n\nNumber of a's in word: "<<number_of_a_s(input)<<"\n";system("Pause");}int number_of_a_s(char value[20]) {int counter=0;for(int i=0;i<20;i++) {    if (value[i]== 'a')        counter++;}return counter;}`

Posts: 137
Joined: Mon Dec 15, 2003 11:03 am
Location: MS --> USA

Madacgrav, that's not recursive and it's broken. Your intentions are probably good, but you are not helping jinx at all.

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

alvaro on "?" i dont know excatly i want to put there. Im still thinking how to approach this problem. I know what to do in english which is go thru all the letters of the word to see if there are any "a" on the word. If there are any count it and display how many "a" i have.
jinx

What you are saying in English is a non-recursive solution. Read my first post and try again; it really has the key to solving the problem.

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

jinx wrote:what i need to do is to check the letters of the array. To see if its a "a" or not.

1.i need to check the first letter of the array if its "a" if not move on to the second letter and so on.

2.my output should say how many "a" i have on the word.

OK, you have a buffer with x characters in it.
1) look at each characters
2) count it if a character is 'a'

Ignore #2 for the moment. How would you
1) display the first character
3) continue until end of buffer

Use a for loop or while loop and step thru the buffer.

FYI, this is a help forum. We try to help people figure out how to solve their problems so they can do it later. We don't give them solutions because they learn nothing. Please refrain from giving answers. You aren't getting the grade for their course.
There are only 10 types of people in the world -- those that use binary, and those that don't
WaltP

Posts: 1187
Joined: Thu Oct 16, 2003 11:24 am

WaltP wrote:Use a for loop or while loop and step thru the buffer.

That's a good way to count the number of `a's in a string. It's just not recursive, and the OP was asking for a recursive solution.

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Alvaro wrote:
WaltP wrote:Use a for loop or while loop and step thru the buffer.

That's a good way to count the number of `a's in a string. It's just not recursive, and the OP was asking for a recursive solution.

sorry, missed that.
WaltP

Posts: 1187
Joined: Thu Oct 16, 2003 11:24 am

here is my current code but im not getting any output

Code: Select all
`#include<iostream.h>#include<string.h>int word(char*);void main(){   char w[30];   cout << "Please enter a word: ";   cin  >> w;    cout << word(w);}int word(char *x){   int ctr = 0;   if( *x != 'a')   {      return word(x+1);   }   else   {      return word(x+1);   }}`
jinx

Next