| | |
Count number of occurrences of a character search it in a file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2007
Posts: 5
Reputation:
Solved Threads: 0
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}
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}
•
•
Join Date: Jul 2007
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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?
thanks for your reply..
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.
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.
•
•
Join Date: Jul 2007
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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?
•
•
•
•
show us how far you can get on your own.
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> using namespace std; int main( ) { int count=0; char letter; ifstream infile; infile .open("Input.txt"); if(! infile) { cout<<"cannot open the input file."<<endl; return 1; } infile>>letter; cout<<"Enter the character:"; while(! infile.eof ( )) { ............................... ............................... ............................. continue; count++; } infile.close( ); return 0; }
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
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:
>>(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!
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)
while( infile >> letter ) { }
>>(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.
It's probably just a contrived way of getting the students to use the keyword, nothing more.
Slightly obfuscated as
C++ Syntax (Toggle Plain Text)
if ( condition ) { doStuff; }
Slightly obfuscated as
C++ Syntax (Toggle Plain Text)
if ( !condition ) continue; doStuff;
•
•
Join Date: Jul 2007
Posts: 5
Reputation:
Solved Threads: 0
hi..
are this correct soultion:
are this correct soultion:
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> #include<string> using namespace std; int main() { int count=0; char letter; string sentense; ifstream infile; infile.open("Input.txt"); if (! infile) { cout<<"cannot open the input file."<<endl; return 1; } cout<<"Enter the character:"; cin>>letter; while(! infile.eof()) { infile>>sentense; continue; count+=letter; cout<<"The letter a repeated"<<count<<"times(s)"<<endl; } infile.close(); return 0; }
Last edited by Ancient Dragon; Aug 7th, 2007 at 1:59 pm. Reason: add language to get line numbers
>>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.
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.
![]() |
Similar Threads
- array program, dont know where to even begin! (C)
- Count number of one's in 32 bits (C)
- Rand Max Problem (C++)
- I still need help with my program!! (Java)
- Character Frequency (Java)
- Another file I/O question (C++)
- Horspool Algorithm for String Matching (C++)
- trouble with counting letter from file, please help. (C++)
- Turn Off Display and Select an Animated Character in Search Companion in Windows XP (Windows tips 'n' tweaks)
Other Threads in the C++ Forum
- Previous Thread: class
- Next Thread: Need help on Dates
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






