943,984 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8868
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 4th, 2007
0

Count number of occurrences of a character search it in a file

Expand Post »
hi, every body..

I have two program I couldn’t solve them
So, can any body help me. please!!


1-Write a program that accepts a character and count number of occurrences in a file.
The file should have some text, and the program count how many times the inputted character repeated in the file and prints the result on the screen.
(Hint: you have to use continue statement in your solution )

Sample input:
Input.txt
I am a student in the university

Sample output:
Enter the character :a
The letter a repeated 2 times(s)


2-Write a program that accepts a character and search it in a file.
The file should have some text, and the program search for the first occurrence of the character and print it’s location on the screen…
(Hint: use break statement to stop the loop)

Sample input:
Input.txt
I am a student in the university

Sample output:
Enter the character to search :a
the first occurrence of a character is at location 3

I am waiting the replies ..

{ thanks all}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rosemary is offline Offline
5 posts
since Jul 2007
Aug 4th, 2007
3

Re: Count number of occurrences of a character search it in a file

I don't see what the problem is. Why can't you write these programs? There must be some specific information you're lacking. What do you need to figure out how to do in order to make progress towards a solution?
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Aug 4th, 2007
0

Re: Count number of occurrences of a character search it in a file

I don't see what the problem is. Why can't you write these programs? There must be some specific information you're lacking. What do you need to figure out how to do in order to make progress towards a solution?
I want to know how to solve these programs..the method..

thanks for your reply..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rosemary is offline Offline
5 posts
since Jul 2007
Aug 4th, 2007
0

Re: Count number of occurrences of a character search it in a file

Can you for example open a text file and print the contents of the text file to the screen?

If you can do that much, then you're prepared for the next step.

If you can't, then we need to clear that bit up for you before progressing with the searching bit of the assignment.

In other words, show us how far you can get on your own.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 5th, 2007
0

Re: Count number of occurrences of a character search it in a file

Click to Expand / Collapse  Quote originally posted by Salem ...
Can you for example open a text file and print the contents of the text file to the screen?

If you can do that much, then you're prepared for the next step.

If you can't, then we need to clear that bit up for you before progressing with the searching bit of the assignment.

In other words, show us how far you can get on your own.
Quote ...
Can you for example open a text file and print the contents of the text file to the screen?
yes,Ican do that..


Quote ...
show us how far you can get on your own.
The code for first program AND the other program like the first one BUT the algorithm(indentation )is different and we must use the break statement :

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. int main( )
  5. {
  6. int count=0;
  7. char letter;
  8.  
  9. ifstream infile;
  10. infile .open("Input.txt");
  11. if(! infile)
  12. {
  13. cout<<"cannot open the input file."<<endl;
  14. return 1;
  15. }
  16. infile>>letter;
  17. cout<<"Enter the character:";
  18. while(! infile.eof ( ))
  19. {
  20. ...............................
  21. ...............................
  22. .............................
  23. continue;
  24. count++;
  25. }
  26.  
  27. infile.close( );
  28. return 0;
  29.  
  30. }

My question is what should be the algorithm(indentation ) in the blank space in two programs????

{thanks all}
Last edited by Ancient Dragon; Aug 5th, 2007 at 9:23 am. Reason: add line numbers to code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rosemary is offline Offline
5 posts
since Jul 2007
Aug 5th, 2007
0

Re: Count number of occurrences of a character search it in a file

lines 16 and 17 need to be reversed because they are in the wrong order. You have to display the question on the screen before you expect someone to enter the answer on the keyboard.

who gave you that code snippet? I sure hope it was not your instructor, because if it was you are in for a lot of problems. Line 18 is not the correct way to write that kind of loop. For the first problem it should be like this:
C++ Syntax (Toggle Plain Text)
  1. while( infile >> letter )
  2. {
  3.  
  4. }

>>(Hint: you have to use continue statement in your solution )
Ridiculous -- no you don't. That problem can be easily solved without using the continue or break statements. All you have to do inside that loop is check if letter is the correct character and increment the counter if it is.

And the second problem is just as stupid as the first. My thoughts: you have an idot for a teacher!
Last edited by Ancient Dragon; Aug 5th, 2007 at 9:33 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 5th, 2007
0

Re: Count number of occurrences of a character search it in a file

It's probably just a contrived way of getting the students to use the keyword, nothing more.

C++ Syntax (Toggle Plain Text)
  1. if ( condition ) { doStuff; }

Slightly obfuscated as
C++ Syntax (Toggle Plain Text)
  1. if ( !condition ) continue;
  2. doStuff;
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 5th, 2007
0

Re: Count number of occurrences of a character search it in a file

Thanks all for your replies..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rosemary is offline Offline
5 posts
since Jul 2007
Aug 7th, 2007
0

Re: Count number of occurrences of a character search it in a file

hi..

are this correct soultion:

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7. int count=0;
  8. char letter;
  9. string sentense;
  10.  
  11. ifstream infile;
  12. infile.open("Input.txt");
  13. if (! infile)
  14. {
  15. cout<<"cannot open the input file."<<endl;
  16. return 1;
  17. }
  18.  
  19. cout<<"Enter the character:";
  20. cin>>letter;
  21. while(! infile.eof())
  22. {
  23.  
  24. infile>>sentense;
  25. continue;
  26. count+=letter;
  27. cout<<"The letter a repeated"<<count<<"times(s)"<<endl;
  28. }
  29.  
  30. infile.close();
  31. return 0;
  32. }
Last edited by Ancient Dragon; Aug 7th, 2007 at 1:59 pm. Reason: add language to get line numbers
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rosemary is offline Offline
5 posts
since Jul 2007
Aug 7th, 2007
0

Re: Count number of occurrences of a character search it in a file

>>are this correct soultion
No. lines 21-28 are incorrect because lines 26 and 27 will never get executed and the while statement at line 21 is wrong.
Last edited by Ancient Dragon; Aug 7th, 2007 at 2:03 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: class
Next Thread in C++ Forum Timeline: Need help on Dates





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC