Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help for a beginner :)

Status
Not open for further replies.

boobkin

MIS
Oct 13, 2011
1
LT
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?
 
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.

Part and Inventory Search

Sponsor

Back
Top