| werlay (47) | |
| @jumper 007 thanks alot but for now am very new to C++ and i think my brain is too mild for ur alog style. I need more sutle ways yo go abt solving c++ probs first then i'll upgrad to ur level. At least i must learn to walk before i can fly. Would keep in contact with you sir. Thanks a lot | |
|
|
|
| werlay (47) | |||
pos is initially zero. but looking at it the counter increase it as it searches thru the string.
here the first substring starts from 0 to 5 and since pos is 0. then the next sub should be pos+5 right | |||
|
|
|||
| werlay (47) | |||
got it.... ****dancing*****
| |||
|
Last edited on
|
|||
| werlay (47) | |||
at last it wasnt necessary to use both functions
| |||
|
Last edited on
|
|||
| werlay (47) | |
| now ow do you print out a word in reverse. like christmas becomes samtsirhc ? | |
|
|
|
| jumper007 (361) | |
|
@werlay Sir, I have to tell you that I've been in your situation exactly one year ago. Our teacher gave us as an assignment almost the same task. Most of my classmates didn't solve it, because they tried to use a lot of complicated things. I, instead, used the KMP algorithm for pattern matching. Not only did I get a 10, but also my teacher congratulated me on using the easiest/most optimal way possible. Comparing the two methods, the KMP, and your method, in my opinion, the KMP is more easier to understand. If you wish, as I said before, I can give you full explanations on the KMP, or if you wish, I can guide you through the same tutorials I've used when I first learnt this algorithm. At least at my school, using already defined algorithms is compulsory. And by learning those algorithms, you actually understand what their "inventor" was thinking of, what his idea was, and you finally develop a good algorithmic thinking, being able to solve further problems without difficulties. I really hope I could help you, ~ Raul ~ EDIT: http://www.cplusplus.com/reference/string/string/reserve/ | |
|
Last edited on
|
|
| werlay (47) | ||
@jumper--thanks alot.would surely keep in touch.
i wanna know about reverse not reserve | ||
|
|
||
| werlay (47) | |
| @JLB and CIRE. thanks guys. still waiting for you on tutorial about reversing a string. | |
|
|
|
| jumper007 (361) | |||||
|
Oh sorry. My fault. Clicked the wrong link. This is the syntax:
EDIT: You can also create your own reverse function using pointers if you want. It would look like this:
| |||||
|
Last edited on
|
|||||
| werlay (47) | |||
@jumper i did something like this.
problem is i get errors | |||
|
|
|||
| jumper007 (361) | |||
Yes.string s3 = reverse(s2.begin(), s2.end()); // WRONG Correct:
~ Raul ~ EDIT: Function reverse doesn't return a value. If you want to pass the reversed string to another one, you have to use the function assign. | |||
|
Last edited on
|
|||
| JLBorges (1756) | |
|
One way to reverse a sequence of length n { a0, a1, a2, ..., an-2, an-1 } is: swap a0 with an-1 swap a1 with an-2 and so on till we reach the middle of the sequence. | |
|
|
|
| jumper007 (361) | ||||
@JLBorges... That's what I said one post before:
~ Raul ~ | ||||
|
Last edited on
|
||||
| JLBorges (1756) | |
|
> @JLBorges... That's what I said one post before: Yes. I was encouraging werlay to think about the general case 'reverse a mutable sequence in place'. Rather than focus on a special case 'reverse a string'. | |
|
|
|
| werlay (47) | |||
@jumper
s2 did not reverse. s3 still has the same string as s2 after the assign function | |||
|
|
|||
| werlay (47) | |
| problem about your other technique is it works with chars, and i wanna reverse a string. so i gotta convert to char first? | |
|
|
|
| werlay (47) | |
| @jumper, @JLB @Cire still waiting on you guys | |
|
|
|
| JLBorges (1756) | |
|
What are you waiting for? 1. Implement reverse (as per the logic given here) http://www.cplusplus.com/forum/general/88575/4/#msg477148 2. Compile the code; fix the compile time errors if any. 3. Once it compiles, run it and test it. 4. If it does not work , modify the code and go back to step 2. If you encounter problems, post the code and ask a question. | |
|
|
|
| werlay (47) | |||
|
that means i'll have to change my strings to reverse into chars, right? what am trying to do is like the previous problem, i wanna check if
you get? | |||
|
Last edited on
|
|||
| JLBorges (1756) | |||
|
> that means i'll have to change my strings to reverse into chars, right? A string is a sequence of chars. If you know how to reverse a sequence, you would be able to reverse a string. > what am trying to do is It can be done in many ways.
But the important issue is: do you clearly understand what these functions are doing? | |||
|
|
|||