473,503 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

i can't find the answer. please help

7 New Member
#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
7 2022
ltgbau
41 New Member
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
9,065 Recognized Expert Moderator Expert
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
ltgbau
41 New Member
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
idle
7 New Member
i've treid already.
but why this error still there.

error C2065: 'strlen' : undeclared identifier
Oct 3 '06 #5
Banfa
9,065 Recognized Expert Moderator Expert
Because you haven't got

#include <string.h>

in your file (at the top preferably)
Oct 3 '06 #6
idle
7 New Member
What is it means by

" missing function header (old-style formal list?)"
Oct 3 '06 #7
Banfa
9,065 Recognized Expert Moderator Expert
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

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

Similar topics

3
4186
by: Mark R | last post by:
I have one .asp page with a SELECT pulldown list on it and some INPUT fields. When SUBMIT is clicked the form data is submitted to that same page and validated. If INPUT fields are empty the asp...
18
1420
by: Stanley J Mroczek | last post by:
I Set the EditCommandColumn to Visible=False to stop people who are not allowed to make any changes to a record. How can set it to Visible=true for some users? Please answer in VB Thanks Stan
2
1748
by: John | last post by:
This message is popping up at startup and its saying "fatal execution engine error (0x7927e03e)" Ive tried deleting, reinstalling frameworks, restoring computer, but I can't use the os cd because i...
3
2929
by: Professor Frink | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
22
3017
by: Tera | last post by:
can you please give me the link ?
9
3314
by: WRH | last post by:
Hello I am new to asp but I made some Jscript functions which work fine. The functions contain some strings used as a registration key for some apps. It is important that these strings not be...
4
1486
by: Gary | last post by:
Can somebody help me get the onMouseOut to work in this script? The onMouseOver works great opening a new window with an image, but I'd like to get the popup window to close onMouseOut. Is this...
2
5059
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
68108
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
8
18123
by: john6630 | last post by:
I am trying to get PDO for sqlite to work on my localhost system. I have modified the PHP5.ini file as shown below and run the following PHP script. As stated below, it reports the mssql, mysql and...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.