473,756 Members | 5,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

14 New Member
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 5713
weaknessforcats
9,208 Recognized Expert Moderator Expert
And your question is....
Aug 4 '07 #2
isabelle
14 New Member
And your question is....
The code for first program AND the other program like the first one BUT the algorithm(inden tation )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(inden tation ) in the blank space in two programs????

{thanks all}
Aug 5 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
isabelle
14 New Member
hi...

where your replies??

I am really need help!!

please, help me??

thanks all
Aug 6 '07 #5
r035198x
13,262 MVP
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
7652
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 directory) and count number of times any given word appears in the text. #!/usr/bin/python
21
2511
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 contains M by 3 matrix of alpha-numeric keys. Where M is number of rows and 3 is number of columns.
4
55415
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
23266
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 to do this? Thanks, Matt
6
5072
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 a new PaperID. The PaperID includes 3 new numbers appended to the original PaperID, e.g. 664.004.1. When I do a count, I do not want to count the reused paper. I set up a count query and had the criteria { Not Like "***.***.*" }. I have also
68
6826
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
2036
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
4946
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 for, and the program is supposed to return the number of occurrences of the string in the file along with the line numbers at which the string occurs, or an error message saying string not found. I've managed to write the code for searching for...
2
3415
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 results of the analysis are to be stored in a suitable data structure. The main task in this project is to design a suitable ADT (call it WAnalysis) to store the results and enable the following operations to be performed as fast as possible:...
0
9271
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10031
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9838
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
8709
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
6534
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
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
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
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2665
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.