Author |
Message |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Fri Apr 08, 2005 3:50 pm Post subject: Clearing spaces in a string |
|
|
Ok, Now if i have a string "QW QW WQ WQ"
How do i make it like this "QWQWWQWQ"
I tried using index and substrings but the thing is i dont know after how many words are spaces located and they are random in strings. No pattern.
Is there a command for it? Or a manual method?
Thanks |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Fri Apr 08, 2005 4:05 pm Post subject: (No subject) |
|
|
What have you got so far? There are a few tricks in this problem. First, do you know how to find a space? Next, how far have you got? Post code!
If you haven't got this already, here's a hint.
code: |
word := word (1 .. spacePosition - 1) + word (spacePosition + 1 .. *)
|
But there's more to it than that. |
|
|
|
|
![](images/spacer.gif) |
Unreal_Origin
![](http://www.geocities.com/i_am_here462002/war3logo.jpg)
|
Posted: Fri Apr 08, 2005 4:15 pm Post subject: (No subject) |
|
|
here it is
code: |
var word : string := "QW QW QW"
fcn no_space (word : string) : string
var newword : string := ""
for i: 1 .. length (word)
if word (i) not = " " then
newword:= newword+word(i)
end if
end for
result newword
end no_space
put no_space (word)
|
there is probably another way to do it but this is what i made |
|
|
|
|
![](images/spacer.gif) |
Drakain Zeil
|
Posted: Fri Apr 08, 2005 4:17 pm Post subject: (No subject) |
|
|
You could make a loop for 1... length of the string
then go newstring+=oldstring
ONLY if the position in the string is not a " ".
You can pull out parts of a string by specifying placement in brackets.
ie.
code: |
stringer:="hi my name is bob"
put stringer (2)
put stringer (2..6)
put stringer (1..*)
|
these output:
"i"
"i my n"
"hi my name is bob"
So just use a check for 1.. string length, if it is equal to a space do nothing, else, add on the letter to the string. |
|
|
|
|
![](images/spacer.gif) |
Bacchus
![](http://img104.exs.cx/img104/9206/ravenmoon12ns.jpg)
|
Posted: Sat Apr 09, 2005 1:03 am Post subject: (No subject) |
|
|
i think the point was to let them figure it out by themselves. the best way i can think of is Cervantes way, which also was a question in that stupid turing book if i remember right. the point is to use index to find the position of the space then set the word to the word leading up to the space (space-1) then add the rest of the word after the space (space+1) and keep repeating that until index returns 0 saying that there wasnt ne more space in there. this way would go faster (i think) cause then it doesnt have to go searching through every single character of the string and checking to se if it was a space |
|
|
|
|
![](images/spacer.gif) |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Sun Apr 10, 2005 7:05 pm Post subject: (No subject) |
|
|
yea i read, i came up with something like this->
code: | var a : string := "UM DID IT WORK?! ?! ?!? !? ?! "
var count : int
loop
for s : 1 .. length (a)
if a (s) = " " then
count := s
exit
end if
end for
if ((count + 1) > length (a)) or (count = length (a)) then
a := a (1 .. count - 1)
else
a := a (1 .. count - 1) + a (count + 1 .. *)
end if
if index (a, " ") = 0 then
put a
exit
end if
end loop
|
|
|
|
|
|
![](images/spacer.gif) |
|