| JLBorges (1756) | |
|
> any suggestions You already know how to read the contents of the file into a string. Now use std::string::find() to find and print out the char offsets at which the token is found.http://www.cplusplus.com/reference/string/string/find/ | |
|
|
|
| cire (2354) | |||
Perhaps something like:
| |||
|
|
|||
| JLBorges (1756) | |||
Or, since the file is very small (8 lines or so):
| |||
|
|
|||
| werlay (47) | |
|
thanks bro.. done. a quick one. what if am to search for ANY 5 letter char that is immediatly repeated in the string. like ( PINRTTTIINNNTTTTIPRINTPRINTPNNNNTRPPNNRI) print is immediately repeated in the string. | |
|
|
|
| werlay (47) | |
| i would also like to know the position it occured and the letters reapeated.. thanks | |
|
|
|
| JLBorges (1756) | |
|
Oh, c'mon. You should be able to do this on your own. Iterate through the string, starting at position 0, picking up sub-strings of length 5 one by one and check if each sub-string is immediately repeated. http://www.cplusplus.com/reference/string/string/substr/ Make an attempt, and if you have problems, post your code and a question. | |
|
|
|
| werlay (47) | |||||
@JLB dont know what am doing wrong. i get a debug assertion failed error when i used the code below.
and then i put in break point with this code btu still gives me the same error.
| |||||
|
Last edited on
|
|||||
| MikeyBoy (235) | |
|
Well if you're hitting the assertion without triggering the breakpoint, the obvious reason is that the assertion is happening before the breakpoint. That should help you narrow down where it's happening. But if you're gettign an assertion, then it should be be really easy to use a debugger and find out exactly where the problem is occurring. | |
|
Last edited on
|
|
| werlay (47) | |
| but is the above code right ? | |
|
|
|
| JLBorges (1756) | |||
|
> dont know what am doing wrong. > i get a debug assertion failed error when i used the code below. Ok, let's take this one baby step at a time. First, implement the simple function
Get it working, and then post the code. We canl then move on to the next step. | |||
|
|
|||
| werlay (47) | ||
thats where the problem is.. writing the code.. dnt knw what code to use... | ||
|
|
||
| JLBorges (1756) | |||
Ok, let's try something simpler.
Use std::string::substr() http://www.cplusplus.com/reference/string/string/substr/ | |||
|
|
|||
| werlay (47) | |
| i get you very well. but the problem i have is that the 5 character string is unknown. i need to get any random 5 letter words that immediately repeats | |
|
|
|
| JLBorges (1756) | |||
Ok. So do this first (before attempting fancier stuff like 'any random 5 letter words').
Post your code. | |||
|
|
|||
| werlay (47) | |||
JLB.... this is what i got. but it only shows the last 3 letters am i right wit it.... | |||
|
|
|||
| cire (2354) | ||
If you were right with it, it would display what you intended it to display. http://www.cplusplus.com/reference/string/string/substr/ If you're unsure how something is used, look it up. I'm not sure what you think you're doing with the char array and the copying, but it's completely unnecessary. You can just use the original string (and even if you wanted to use a new one string str(input); would've done the trick.) Also there was no reason to count the number of characters in the string since input.length() returns that directly.
| ||
|
|
||
| werlay (47) | |||
Okay jlb i got to understand how the substring works now. so i moved on with my code and tried to find the repeated 5 letter word but am not sure of the code.. help me see whats wrong.
| |||
|
|
|||
| werlay (47) | |
|
i keep getting a debug assertion failure and its not working | |
|
|
|
| JLBorges (1756) | |||||||
To check id two std::string objects are equal, use the == operator. if( str1 == str2 ) { /* str1 and str2 are equal */ }So, the function can be written as
Or simply:
Now, write this function:
And post your code. | |||||||
|
|
|||||||
| werlay (47) | |||
thats my code.. what next sir? | |||
|
|
|||