By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,701 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

i can't find the answer. please help

P: 7
#include<stdio.h>
int count (char letter, const char *my_string);
void main()
{
int total_l;
char my_string[] = “walla la oh la la la la la long”;
char letter = ‘l’;
total_l = count(letter, my_string);
printf(“There are %d character l in the string\n”, total_l);
}

*required to write a recursive function to count the number of times character ‘l’
appears in a string “walla la oh la la la la la long”

* prints that number on your screen. Your function should be able to;

(a) Count character ‘l’.
(b) Test whether the character ‘l’ is in the string.
(c) If the string had no character at all, we would know immediately that there
were zero occurrences of the character being counted.
(d) If there is any, your program should track the number of occurrence.
Oct 3 '06 #1
Share this Question
Share on Google+
7 Replies


P: 41
Expand|Select|Wrap|Line Numbers
  1. int count(char ch, char str[])
  2. {
  3.     int cnt=0;
  4.     if(strlen(str)==1)
  5.     {
  6.         if(str[0]==ch)
  7.             cnt++;
  8.     }
  9.     else
  10.     {
  11.         if(str[0]==ch)
  12.             cnt++;
  13.         cnt+=foo(ch, str+1);
  14.     }
  15.     return cnt;
  16. }
  17.  
Oct 3 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. int count(char ch, char str[])
  2. {
  3.     int cnt=0;
  4.     if(strlen(str)==1)
  5.     {
  6.         if(str[0]==ch)
  7.             cnt++;
  8.     }
  9.     else
  10.     {
  11.         if(str[0]==ch)
  12.             cnt++;
  13.         cnt+=foo(ch, str+1);
  14.     }
  15.     return cnt;
  16. }
  17.  
You've called foo instead of count.

I think you will find this goes rather wrong for

count( 'l', "");

which is a specific return case mentioned.
Oct 3 '06 #3

P: 41
yes, the condition is not strict enough :D
i don't know why he needs a recursive function to count the number of times character ???

sorry for my mistake :)




Expand|Select|Wrap|Line Numbers
  1.  int count(char ch, char str[])
  2.  
  3.  
  4. {
  5. int cnt=0;
  6. int len=strlen(str);
  7. if(len<=0)
  8. {
  9. return 0;
  10. }
  11. else if(len==1)
  12. {
  13. if(str[0]==ch)
  14. cnt++;
  15. }
  16. else
  17. {
  18. if(str[0]==ch)
  19. cnt++;
  20. cnt+=count(ch, str+1);
  21. }
  22. return cnt;
  23. }


Oct 3 '06 #4

P: 7
i've treid already.
but why this error still there.

error C2065: 'strlen' : undeclared identifier
Oct 3 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
Because you haven't got

#include <string.h>

in your file (at the top preferably)
Oct 3 '06 #6

P: 7
What is it means by

" missing function header (old-style formal list?)"
Oct 3 '06 #7

Banfa
Expert Mod 5K+
P: 8,916
Often a ; where there shouldn't be one

Expand|Select|Wrap|Line Numbers
  1. void fn();
  2. {
  3. }
  4.  
should be

Expand|Select|Wrap|Line Numbers
  1. void fn()
  2. {
  3. }
  4.  
Oct 3 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.