Computer Science Canada String Manipulation :D |
Author: | Synix [ Thu Nov 13, 2008 10:59 am ] | ||||
Post subject: | String Manipulation :D | ||||
Hey guys! Before you ask, yes I did read the tutorial on string manipulation but it didn't answer my question... Here's my program so far...
The first 2 options I have no trouble with. I just can't seem to figure out how to substitute a pattern in the 3rd option... Any help is much appreciated, I'd like to expand my knowledge ![]() Mod Edit: Remember to use syntax tags. Thanks ![]()
|
Author: | pavol [ Thu Nov 13, 2008 12:58 pm ] |
Post subject: | RE:String Manipulation :D |
Your program seems to work fine, all you need is your exit condition for loop in your 3rd option. Let me ask you this: what does index() return when a pattern is not found within a word? i.e., when you have already replaced all occurrences of a pattern within the word. |
Author: | Synix [ Fri Nov 14, 2008 10:08 am ] | ||
Post subject: | RE:String Manipulation :D | ||
I got it! ![]() FIXED
|
Author: | Synix [ Fri Nov 14, 2008 10:26 am ] | ||
Post subject: | RE:String Manipulation :D | ||
Ok, I fixed the program but now I have to replace the old pattern with the newpattern x2. (ex. input moon, pattern o, replace with pattern aa, = maaaan) So I figured I was smart when I made this change here...
It worked but I ran into a problem when trying to use moon as the word, o as the pattern, and o as the pattern replacing the pattern (so that it would output as moooon). Any help is much appreciated and thanks for the quick reply ;] *EDIT* The error I get is... "String generated by string catenation too long." |
Author: | pavol [ Fri Nov 14, 2008 11:22 am ] |
Post subject: | Re: String Manipulation :D |
good job on the first part. now with the next problem, the thing is if you take your word 'moon', and want to replace the o with oo, then the first time around your program will make the word mooon, then moooon, mooooon...etc, since there will always be more o's in your word because you keep adding them, your loop will not exit until it gives the error "String generated by string catenation too long.". what you could do instead is keep track of which 'o' you are replacing by having a variable store the position you have checked, and every time you replace a pattern, increment that variable until you get to the end of the word. |
Author: | Synix [ Mon Nov 17, 2008 10:42 am ] |
Post subject: | RE:String Manipulation :D |
Uggh I still can't seem to get it... I don't know how to store the position of the pattern I'm replacing. |
Author: | Synix [ Tue Nov 18, 2008 10:25 am ] | ||
Post subject: | RE:String Manipulation :D | ||
I think I'm getting a bit closer... I can turn moon into moooon, but it failed when I tried moomoo -> moooomooooo Check it out
|
Author: | gitoxa [ Tue Nov 18, 2008 5:32 pm ] |
Post subject: | RE:String Manipulation :D |
Have you considered storing the value of your index function before modifying the string? |
Author: | Synix [ Wed Nov 19, 2008 10:20 am ] | ||
Post subject: | RE:String Manipulation :D | ||
My friend wrote this program in a totally different way. I don't understand the logic behind it so I'm not going to use it. Here it is...
I know there must be a way to fix my program to do the same thing I just can't seem to figure it out. |
Author: | cat8864 [ Thu Nov 20, 2008 1:19 pm ] |
Post subject: | Re: String Manipulation :D |
I think I get it. The part "word (i) " records the last place checked against the pattern - if you remove it then the 'm' and 'n' in moon are replaced "newWord" is the results of the last check - if the pattern to be replaced was not at the last spot checked the letter isn't changed. If the pattern to be replaced was there then the replacement is added to the already processed part of the pattern |