446,231 Members | 1,701 Online
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

 P: 7 #include 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
7 Replies

 P: 41 Expand|Select|Wrap|Line Numbers int count(char ch, char str[]) {     int cnt=0;     if(strlen(str)==1)     {         if(str[0]==ch)             cnt++;     }     else     {         if(str[0]==ch)             cnt++;         cnt+=foo(ch, str+1);     }     return cnt; }   Oct 3 '06 #2

 Expert Mod 5K+ P: 8,916 Expand|Select|Wrap|Line Numbers int count(char ch, char str[]) {     int cnt=0;     if(strlen(str)==1)     {         if(str[0]==ch)             cnt++;     }     else     {         if(str[0]==ch)             cnt++;         cnt+=foo(ch, str+1);     }     return cnt; }   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  int count(char ch, char str[])     { int cnt=0; int len=strlen(str); if(len<=0) { return 0; } else if(len==1) { if(str[0]==ch) cnt++; } else { if(str[0]==ch) cnt++; cnt+=count(ch, str+1); } return cnt; } 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

 Expert Mod 5K+ P: 8,916 Because you haven't got #include 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

 Expert Mod 5K+ P: 8,916 Often a ; where there shouldn't be one Expand|Select|Wrap|Line Numbers void fn(); { }   should be Expand|Select|Wrap|Line Numbers void fn() { }   Oct 3 '06 #8