| cire (2354) | |
bool five_chars_are_repeated( const std::string& str, std::size_t pos ) ;Was the function you were meant to implement. Did you test your code? What value do you suppose pos holds when you use it on line 4? Why are you returning 0 after you've returned true or false? If you don't understand what you're doing, ask for clarification. Don't just make stuff up in the hopes that someone will eventually write it for you. Think about what you're doing. | |
|
|
|
| cire (2354) | |
| Oops - double post. | |
|
Last edited on
|
|
| werlay (47) | |
| obviously am very confused, need clarification pleaseeee. | |
|
|
|
| werlay (47) | |||
was wondering where is pos supposed to get its initialized value from? | |||
|
|
|||
| cire (2354) | |
bool five_chars_are_repeated( const std::string& str, std::size_t pos ) ;In the above prototype (posted now for the third time) pos gets it's value from the calling code. | |
|
|
|
| werlay (47) | |
|
Am sorry it sounds embarrasing that still i dont really understand. if pos gets its value from its calling code, that means pos should have been initialized somewhere in the main. am askin ? | |
|
|
|
| cire (2354) | ||||
In a sense. There need be no variable named pos in main, and the code needn't be called only from main. Here is one way you might use the function:
| ||||
|
|
||||
| werlay (47) | |
|
Honestly i appreciate the time you guys are taking to put me through this.but at this point am totally confused. 1. string str; is supposed to be the whole string to be searched. right? 2. in the immediate code above, the function (five_chars_are_repeated) is being called in the function (any_five_chars_are_repeated)? 3. i still dont get where pos gets its value from atol. cos i get an error saying pos is uninnitialized | |
|
Last edited on
|
|
| cire (2354) | ||||
"str" is the name given to the argument fed to any_five_chars_repeated and also the argument fed to five_chars_are_repeated. They refer to the same variable, although if we renamed str to abracadabra in five_chars_are_repeated and left it as str in any_five_chars_repeated, it wouldn't make any difference; the name of a reference doesn't have any direct relationship to what it aliases. They also happen to refer to the variable in main known as input, although they could refer to any variable that is fed to the function.
Yes.
You, apparently, still haven't paid any attention to the function prototype that has been posted four times which differs from the one you're trying to implement. Change the prototype to match. In the code above, pos is initialized with the value of i in any_five_chars_repeated. | ||||
|
|
||||
| werlay (47) | |
| Must i creat two functions to get it done? Because thats what is getting me confused | |
|
|
|
| JLBorges (1756) | |||
|
> Must i create two functions to get it done? Four functions including main()
| |||
|
|
|||
| werlay (47) | |
| Whooop!!!!!!! Its the one with the size_t thats giving me the confusion really | |
|
|
|
| werlay (47) | |||
JLB/CIRE....... this is wat i came up with. running it gives me a position of any five repeated. but am still in doubt of the code. am i on the right track? pleassseee
| |||
|
|
|||
| jumper007 (361) | |||
|
I know this might not actually help, but I'd rather use the KMP (Knuth-Morris-Pratt) Algorithm for pattern matching in this case. Here's it:
| |||
|
|
|||
| JLBorges (1756) | |||||
@werlay:
| |||||
|
Last edited on
|
|||||
| jumper007 (361) | ||||||
Actually, this is how I solved it (for .txt files).
Works like a charm. I hope it helps you. PS: It might be a little different from your task, but I did this one to prove the power of the Knuth-Morris-Pratt pattern matching algorithm. This algorithm saved me from hours of coding not only once. PS 2: Using the std::string::append allows you to put the whole file into one single string. Only after that can you actually write the pattern matching code.EDIT: The file "text.txt" is your "file_name.dat", the "search.txt" holds the word which you are looking for, while the "KMP.txt" holds the result, all the positions of the found matches. EDIT 2: I hope I was clear, but if not, I will explain each line in detail. EXAMPLE: text.txt
search.txt
KMP.txt
Though, the KMP algorithm is a case-sensitive pattern matching algorithm, so whatever word/phrase you enter, you have to pay attention to upper or lower case letters. ~ Raul ~ | ||||||
|
Last edited on
|
||||||
| SamuelAdams (321) | |
|
jumper007, There are no comments in your code. nothing to explain what string K and S are, what goes in the text files, how to make the program work or what it's doing. | |
|
|
|
| jumper007 (361) | |
|
Check it now. It's edited with explanations. ~ Raul ~ PS: I really don't understand why some people try to "re-invent the wheel", when there are already fully optimized algorithms for specific things (KMP being one of them), but whatever, it's everyone's choice whether to use an algorithm or their own code. PS 2: The only reason for which I wrote the code was that no one actually solved the problem from what I saw, and even if someone did, it was not the optimal solution to it (I just checked if any of the posts had a solution using KMP, and since they didn't, it is not optimal). PS 3: I guess @werlay can close the topic. If you need a full explanation (on KMP), write me in PM and I'll gladly answer you. | |
|
Last edited on
|
|
| werlay (47) | |||||
@JLB
: can i do without this part?
i couldnt find the logical error here. | |||||
|
|
|||||
| JLBorges (1756) | |
bool five_chars_are_repeated( const std::string& str, std::size_t pos ) ;Take a sheet of blank paper and a pencil. Now, walk through your logic, writing down what is happening. For example: > can i do without this part? Let str be "abcd12345yz" (11 chars) and pos be 16. Is a check for string length required? Let str be "abcd1234512345z" (15 chars) and pos be 4. What should the check for string length be? etc. > i couldnt find the logical error here Let str be "abcd1234512345xyz", and pos be 4 Which are the two substrings to be compared? At what position does the second substring start? Is it pos+1 (4+1)? etc. | |
|
|
|