How to count repeating lines in a txt file [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So if i have a large text file with lines that repeat, how can I determine the line that repeats the most frequently?
Example
The dog is brown
The cat is orange
The dog is brown
This should return 2 for The Dog is brown

Since this appears to be a learning exercise, here is an approach that you can take:
Make an associative container, say, map<string,int> or unordered_map<string,int> to keep counts
Read the file line-by-line. For each line that you get increment the count in your associative container
Once you finished reading the file, walk the container, find the highest count, and store its associated key
When you are done with the loop, the key that you found and its associated count give the answer to your problem.
One way to understand this approach is to consider the same problem, but with strings replaced with numbers. Now all you need to do is writing a loop performing count[number]++ for each number in the list. The approach described above is essentially the same thing, but you use a string instead of the number for your index.

Related

How do I sort int without stl liberaries [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
I have a problem where I need to sort buses arriving at a bus station on the basis of time of arrival without using STL (standard template library) in ascending order
You may first want to read about sorting algorithms in general. A good staring point is here.
There you see many of them.
The recommendation for newbies is to start with bubble sort.
Please see here for an example including source code.
Then, you need to store your bus data in a struct. Along with the timing information. All those struct shoulb be stored in an array, best a std::vector.
Then you need to write a compare function for times. The complexity of this depends, if you have one varaible that stores the complete time, like in a unix timestamp, or in a struct, for example tm. Then you need to compare hours, minutes and seconds and some boolean relation.
But first, you need to read a lot, then think even longer on how to implement, and then write the code.

How to compare a list of data from a text file to an array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am making a program to calculate student's score. I have data stored inside a text file (fstream) that contains student id and their answer (True/False). Then, I want the program to compare the student's answer inside the file with the correct one (stored inside array) and if it is matched, score will +1 and if not score will -1. (Comparing T/F char by char)
So, for the correct answer of the test I store it inside array.
eg: char ans[5]={'T','F','T','F','T'}
How to compare each line of data inside the file (student's answer) with the array (correct answer)? I just need some hints, right now I don't even know how to start the code yet.
You can read files using file streams in <fstream>.
Once the file open, you can use >> exactly as you would do from the console.
You can then read the student's answers as a string (e.g."FFTTF"). To verify the results, you need to do loop, comparing successively each character of the string at index [i] with the item of your array at the same index.
Increment the score by one for every successful match and you ahve the score.
Note: I won't produce code in order not to spoil your homework. If you edit your question showing what you have tried, you'd get more responses

C++ compare two sentences [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
randomly came across this:
Develop an algorithm to compare two sentences to see if
they match or not. The key aspect of these sentences is that
the words could be in any order (e.g. "california is hot" and "
hot is california" are two sentences that would match).
any ideas?
Parse each sentence into words, use space as delimiters.
Add all std::string words to a std::vector<std::string>, then sort.
Use the ==operator to compare the two vectors for equality.
Perhaps put words into a std::map<string, int> and count up the element each time you find a word on the one side, and down on the other side, then iterate over the map and check that all entries are zero. [This assumes that "california is hot hot" isn't supposed to be the same as "hot is california", in which case you need a bit more logic, to only count words the first time you see them on each side]
Or put each word in each sentence into a std::vector<string>, then sort each vector and compare the two vectors. Again, strategy changes if the sentence needs to be recognised regardless of the number of times each word is seen.

How to split a long input into multiple items in C++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm new to C++ and have been trying to code a program where I enter many things at once and it splits them into different strings, ints, etc. depending on what they are but I can't figure out how.
I want to be able to input, for example, "What is 7 plus 9?" and code it so it assigns the first number (7) to int a, the second number (9) to int b, recognize the word "plus" to be the operator. This would then go to a simple calculator program which I have already coded fine and would then output the answer.
How do I code it so it can split the entire input into multiple individual items? I was thinking there must be some function for a format of input in which case I could define input as string1, string2, int a, string 3, int b, and just have it know the start/end of each by the spaces. Anything you have in mind that could do this please let me know.
Thanks
ideally a lexer and parser like antlr or lex/yacc would be great but the learning curve is steep

Count char occurs in string with length 10^200 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string on Linux standard terminal input. Its maximal length is 10^200 (as said in program specification). I have to count, how many "3" characters are inside it (occur in this string). I couldn't do that by for loop, because there is no so big variable type, which can be used as iterator. Is there any way to analyze so big strings?
Is there any way to analyze so big strings?
Not in this universe there is not. Such an entity cannot exist in this universe1 and that which does not exist, cannot be analyzed.
1 Current estimates of this universe's total number of particles particles are in the region of 1080.
As from your comment
The data source is standard terminal input.
Then you'll need a lot of monkeys to type this in.
Though you don't need to read in what's typed at once into a big string, but you can simply analyze char by char as typed. The std::istream::get(char_type& ch) method is suitable for doing so.