From: Jose Calderon-Celis Date: 2011-04-22T16:45:40+09:00 Subject: Re: Reversing a string without using array, classes and reverse function --20cf302d4d4e38609804a17d0b17 Content-Type: text/plain; charset=ISO-8859-1 #!/usr/bin/env ruby # require 'rubygems' puts "ruby #{RUBY_VERSION}" s = "This is to test reverse of a string" p s len=s.length mycommand="" for j in 1..len do mycommand += s[-1*j] end p mycommand Jose Calderon-Celis 5199906-7970 http://www.tm.com.pe/mensajes/ 2011/4/22 Rubist Rohit > I attempted below given code, but it is neither displaying result nor > error: > > ================CODE========================== > s = "This is to test reverse of a string" > len = s.length > for j in len..1 do > mycommand = "s.scan(/.$/) {|x| puts x}" > mycommand = mycommand.insert 7,"." > end > ============================================== > > What I am doing is to insert a period (.) in the seventh or eighth > position on each loop. > > > spiralofhope wrote in post #994433: > > On Fri, 22 Apr 2011 12:54:16 +0900 > >> I am trying this: > >> > >> mystring = gets > >> mystring.scan(/..$/) {|x| puts x} > >> > >> It returns only the last character. Is it possible to add the above > >> line in loop? > > > > Here's something I stumbled through which seems to work. > > > > - Using a regex of /.$/ > > - Slowly chomping away at the original string. > > - Using another variable to build my result. > > > > mystring = 'Hello, World!' > > result = '' > > > > fail = 0 > > until fail == "100" or mystring == '' do > > fail += 1 > > mystring.match( %r{(.$)} ) > > break if $~ == nil > > result += $~[1] > > mystring = mystring.chomp( $~[1] ) > > end > > > > puts result > > -- > Posted via http://www.ruby-forum.com/. > > --20cf302d4d4e38609804a17d0b17--