Need help: Using Recursion function to count a letter

Ask for help with your homework/assignments in this forum!

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

Need help: Using Recursion function to count a letter

Postby jinx » Tue Jan 06, 2004 11:01 pm

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
 

Postby MXP » Tue Jan 06, 2004 11:58 pm

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

Postby Alvaro » Wed Jan 07, 2004 9:35 am

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"
...
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby jinx » Mon Jan 12, 2004 1:05 am

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
 

Postby WaltP » Mon Jan 12, 2004 3:35 am

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

Postby jinx » Mon Jan 12, 2004 10:34 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
 

Postby Alvaro » Mon Jan 12, 2004 11:43 am

Explain what you want to put instead of `?' in your code. And the function should take a pointer to char, not a char.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby Madacgrav » Mon Jan 12, 2004 1:30 pm

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;
}
Madacgrav
 
Posts: 137
Joined: Mon Dec 15, 2003 11:03 am
Location: MS --> USA

Postby Alvaro » Mon Jan 12, 2004 1:37 pm

Madacgrav, that's not recursive and it's broken. Your intentions are probably good, but you are not helping jinx at all.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby jinx » Mon Jan 12, 2004 4:04 pm

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
 

Postby Alvaro » Mon Jan 12, 2004 8:20 pm

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.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby WaltP » Mon Jan 12, 2004 8:40 pm

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
2) skip to the next character
3) continue until end of buffer

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

Madacgrav
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

Postby Alvaro » Mon Jan 12, 2004 8:47 pm

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.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby WaltP » Mon Jan 12, 2004 9:14 pm

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

Postby jinx » Tue Jan 13, 2004 12:31 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

Return to Homeworks

Who is online

Users browsing this forum: Google [Bot] and 1 guest