By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,558 Members | 1,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,558 IT Pros & Developers. It's quick & easy.

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

P: 14
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}
Aug 4 '07 #1
Share this Question
Share on Google+
5 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
And your question is....
Aug 4 '07 #2

P: 14
And your question is....
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 :

Expand|Select|Wrap|Line Numbers
  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}
Aug 5 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
I'm not sure what line 16 is for.

However, you should:
1) ask for a character from the user
2) enter your loop and read a char from the file.
3) compare the char to the one the user entered.
4) if equal, increment the occurrence count
5) go back to 2.

I'm not supposed to give you the exact code.

Hint: you might want to use cin.get() rather than cin>>.
Aug 5 '07 #4

P: 14
hi...

where your replies??

I am really need help!!

please, help me??

thanks all
Aug 6 '07 #5

10K+
P: 13,264
hi...

where your replies??

I am really need help!!

please, help me??

thanks all
Post #4 in this thread
Aug 6 '07 #6

Post your reply

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