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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 5
Reputation: rosemary is an unknown quantity at this point 
Solved Threads: 0
rosemary rosemary is offline Offline
Newbie Poster

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

 
0
  #1
Aug 4th, 2007
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}
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

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

 
3
  #2
Aug 4th, 2007
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?
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 5
Reputation: rosemary is an unknown quantity at this point 
Solved Threads: 0
rosemary rosemary is offline Offline
Newbie Poster

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

 
0
  #3
Aug 4th, 2007
Originally Posted by Rashakil Fol View Post
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..
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #4
Aug 4th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 5
Reputation: rosemary is an unknown quantity at this point 
Solved Threads: 0
rosemary rosemary is offline Offline
Newbie Poster

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

 
0
  #5
Aug 5th, 2007
Originally Posted by Salem View Post
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.
Can you for example open a text file and print the contents of the text file to the screen?
yes,Ican do that..


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 :

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,616
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #6
Aug 5th, 2007
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:
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #7
Aug 5th, 2007
It's probably just a contrived way of getting the students to use the keyword, nothing more.

  1. if ( condition ) { doStuff; }

Slightly obfuscated as
  1. if ( !condition ) continue;
  2. doStuff;
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 5
Reputation: rosemary is an unknown quantity at this point 
Solved Threads: 0
rosemary rosemary is offline Offline
Newbie Poster

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

 
0
  #8
Aug 5th, 2007
Thanks all for your replies..
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 5
Reputation: rosemary is an unknown quantity at this point 
Solved Threads: 0
rosemary rosemary is offline Offline
Newbie Poster

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

 
0
  #9
Aug 7th, 2007
hi..

are this correct soultion:

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,616
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #10
Aug 7th, 2007
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC