String Manipulation :D
Author |
Message |
Synix
|
Posted: 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...
Turing: |
var num, size, pos, count, l : int
var pattern, newpattern, word, newword2 : string
put "Menu"
put "1. Count a pattern"
put "2. Eliminate a pattern"
put "3. Substitute a pattern"
put "4. Exit"
get num
if num = 1
then
count := 0
cls
put "Enter the single letter pattern you want to count"
get pattern
put "Enter a word containing the pattern"
get word
size := length (pattern )
loop
pos := index (word, pattern )
exit when pos = 0
count := count + 1
word := word (pos + size .. *)
end loop
put "The number of occurences is: ", count
elsif num = 2
then
cls
put "Enter the pattern you would like to eliminate"
get pattern
put "Enter the word in which you would like to eliminate a pattern from"
get word
var newword := ""
var i : int
size := length (pattern )
loop
pos := index (word, pattern )
exit when pos = 0
word := word (1 .. pos - 1) + word (pos + size .. *)
end loop
put "Here is the word without the pattern: ", word
elsif num = 3
then
cls
loop
put "Enter the word you want to replace a pattern in"
get word
put "Enter the pattern you would like to search for within the word"
get pattern
put "Enter the pattern you would like to replace the old pattern with within the word"
get newpattern
loop
word := word (1 .. index (word, pattern ) - 1) + newpattern + word (index (word, pattern ) + 1 .. *)
exit when
end loop
put word
end loop
elsif num = 4
then
put "Go <3 yourself"
end if
|
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 code: | [syntax="Turing"]Code here[/syntax] |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
pavol
|
Posted: 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. |
|
|
|
|
|
Synix
|
Posted: Fri Nov 14, 2008 10:08 am Post subject: RE:String Manipulation :D |
|
|
I got it!
FIXED
Turing: | elsif num = 3
then
cls
loop
put "Enter the word you want to replace a pattern in"
get word
put "Enter the pattern you would like to search for within the word"
get pattern
put "Enter the pattern you would like to replace the old pattern with within the word"
get newpattern
loop
word := word (1 .. index (word, pattern ) - 1) + newpattern + word (index (word, pattern ) + 1 .. *)
exit when index (word, pattern ) = 0
end loop
put word
put ""
end loop |
|
|
|
|
|
|
Synix
|
Posted: 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...
Turing: |
loop
put "Enter the word you want to replace a pattern in"
get word
put "Enter the pattern you would like to search for within the word"
get pattern
put "Enter the pattern you would like to replace the old pattern with within the word"
get newpattern
newpattern:=newpattern+newpattern
loop
word := word (1 .. index (word, pattern ) - 1) + newpattern + word (index (word, pattern ) + 1 .. *)
exit when index (word, pattern ) = 0
end loop
put word
put ""
end loop
|
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." |
|
|
|
|
|
pavol
|
Posted: 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. |
|
|
|
|
|
Synix
|
Posted: 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. |
|
|
|
|
|
Synix
|
Posted: 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
Turing: |
elsif num = 3
then
cls
loop
put "Enter the word you want to replace a pattern in"
get word
put "Enter the pattern you would like to search for within the word"
get pattern
put "Enter the pattern you would like to replace the old pattern with within the word"
get newpattern
%newpattern:=newpattern+newpattern
times := length (newpattern )
put times
for : 1 .. times
count := count + 1
put count
word := word (1 .. index (word, pattern ) - 1) + newpattern + word (index (word, pattern ) + 1 .. *)
pos := index (word, pattern )
exit when times = count
%or count = length(newpattern) or index (word, pattern) = 0 or pos = length (word)
end for
put word
put ""
count := 0
word := ""
end loop
|
|
|
|
|
|
|
gitoxa
|
Posted: 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? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Synix
|
Posted: 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...
Turing: |
var word, newword, pattern, newpattern : string: = ""
put "Enter the word"
get word
put "Enter the pattern"
get pattern
put "Enter the replacement"
get newpattern
for i : 1 .. length(word )
if index(word (i ), pattern )= 0
then
newword:=newword+word (i )
else newword:=newword+newpattern
end if
end for
put newword
|
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. |
|
|
|
|
|
cat8864
|
Posted: 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 |
|
|
|
|
|
|
|