# Need help for a beginner :)

Status
Not open for further replies.

#### boobkin

##### MIS
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
Any ideas?

to get help, you'd better convince people this isn't homework. It's a standard exercise set at the begining of many courses, and it's the time of year when many courses are just begining.

Hi

I agree with lionelhill, this quite smells like homework. However an interesting one, given the spaces which seems to need to be preserved.
Code:
``````[gray]// set the reversed string to the original, but replace the groups of non-space characters with placeholders, for example \$[/gray]
original := ' one  two   three    four'
reversed := ' \$  \$   \$    \$'

currently_in_a_word := no [gray]// in original we are inside a group of non-space characters[/gray]
current_placeholder := 0 [gray]// in reversed this is the current place_holder position[/gray]
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 [gray]// not a space[/gray]
if not currently_in_a_word [gray]// first character of a non-space group[/gray]
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.

Status
Not open for further replies.

Replies
2
Views
146
Replies
1
Views
114
Replies
18
Views
211
Replies
1
Views
139
Replies
17
Views
210