473,657 Members | 2,624 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 2035
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
4190
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 code will insert a visual indicator (e.g. *) to convey that the field must be filled in. However, if the user has made a selection in the pulldown list and has left an INPUT field empty, when the form reappears after validation, the pulldown menu...
18
1432
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
1758
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 hvae important programs on my system that would cost alot to replace. I noticed a sql server native client isnt there like it was before but cant find same program anywhere. Please I hope someone can help me out. I have a web page that im trying...
3
2951
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 application that is going to be used to extract data from a legacy system (VSAM based mainframe file structure), and output data in pipe-delimited record layouts, multiple record types per file, one file per chosen client. I have been working on...
22
3043
by: Tera | last post by:
can you please give me the link ?
9
3335
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 visible to a client using a browser. My question is...can a knowledgeable browser user view Jscript source code in an asp file?
4
1491
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 possible? I'm guessing it's not because the parent window loses focus when the popup opens. Is there a better way to do this? I have thumbnails of my kids that I want to open in a larger format when hoverer over so the grandparents can see them...
2
5074
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 must be embedded somewhere in the source code, and I would like to know if anyone knows where to find the correct answer. I would greatly appreciate it!! Thanks!! <!--put the preloads file here as it must load before the website class...
4
68195
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 must be embedded somewhere in the source code, and I would like to know if anyone knows where to find the correct answer. I would greatly appreciate it!! Thanks!!
8
18147
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 sqlite2 drivers but then gives a "could not find driver" exception. Any help is greatly appreciated. Also, I am concerned if PDO for sqlite is widely supported by hosting companies, any input or opinions on that? <?php...
1
8522
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5647
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.