Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Clearing spaces in a string
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MysticVegeta




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: 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! Smile

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.
Unreal_Origin




PostPosted: 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
Drakain Zeil




PostPosted: 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.
Bacchus




PostPosted: 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
MysticVegeta




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: