473,385 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
5 5692
weaknessforcats
9,208 Expert Mod 8TB
And your question is....
Aug 4 '07 #2
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
9,208 Expert Mod 8TB
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
hi...

where your replies??

I am really need help!!

please, help me??

thanks all
Aug 6 '07 #5
r035198x
13,262 8TB
hi...

where your replies??

I am really need help!!

please, help me??

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

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

Similar topics

5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
21
by: rccf6178 | last post by:
Hi all, Do anyone know how to write a C program to count the frequency of a certain key pattern in a given datafile? Input: • Three alpha-numeric keys separated by space • A data file...
4
by: thomaz | last post by:
Hi.... There is a string method to count the total number of a specified character in a string. EX: count the total of (*) in a string *** Test *** Thanks....
14
by: mesterak | last post by:
I want to very quickly count the number of lines in text files without having to read each line and increment a counter. I am working in VB.NET and C#. Does anyone have a very fast example on how...
6
by: dBNovice | last post by:
Hey group, I am trying to do a count of the number of papers in a table. The table has a PaperID that differentiates each paper , e.g. 004.1. Some papers are reused. The reused paper is given...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
9
by: nick048 | last post by:
Hi to All, I need to search the string occurencies in a structure like this: struct node { char info; struct node *next; };
3
by: Beeteh | last post by:
Hello everybody! I'm a so-called newbie at programming in C/C++ and I'm currently working on a poject that searches for a particular string in a file. The user enters the string he/she is searching...
2
by: alwaali | last post by:
Hi I need help please This is my project and i need a help to solve it with you A page of text is to be read and analyzed to determine number of occurrences and locations of different words. The...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.