Need help for a beginner :)
Need help for a beginner :)
(OP)
Hi all. I can't figure out how to do this:
I have a String e.g. '1 2 3 4 '
I need to get this: '4 3 2 1 '
Or i have: ' one two three four'
Need to get this: ' four three two one'
And I need to do that WITHOUT arrays o.O
Any ideas?
I have a String e.g. '1 2 3 4 '
I need to get this: '4 3 2 1 '
Or i have: ' one two three four'
Need to get this: ' four three two one'
And I need to do that WITHOUT arrays o.O
Any ideas?
RE: Need help for a beginner :)
RE: Need help for a beginner :)
I agree with lionelhill, this quite smells like homework. However an interesting one, given the spaces which seems to need to be preserved.
CODE --> pseudocode
original := ' one two three four'
reversed := ' $ $ $ $'
currently_in_a_word := no // in original we are inside a group of non-space characters
current_placeholder := 0 // in reversed this is the current place_holder position
loop backward through original character by character
current_character := original's current character
if current_character is a space
if currently_in_a_word
currently_in_a_word := no
end if
else // not a space
if not currently_in_a_word // first character of a non-space group
currently_in_a_word := yes
advance current_placeholder to the next one
remove the placeholder character at current_placeholder
end if
insert current_character in reversed at current_placeholder
end if
end loop
Feherke.