Search code examples
rubyfunctionruby-on-rails-3eachalphabet

Increment alphabetical string in Ruby on rails


Task I want to solve:

Write a program that takes a string, will perform a transformation and return it. For each of the letters of the parameter string switch it by the next one in alphabetical order. 'z' becomes 'a' and 'Z' becomes 'A'. Case remains unaffected.

def rotone(param_1)
  a = ""
param_1.each_char do |x|
    if x.count("a-zA-Z") > 0
        a << x.succ
    else
        a << x
    end
end
   a
end

And I take this:

Input:  "AkjhZ zLKIJz , 23y "
Expected Return Value: "BlkiA aMLJKa , 23z "
Return Value:          "BlkiAA aaMLJKaa , 23z "

When iterators find 'z' or 'Z' it increment two times z -> aa or Z -> AA


Solution

  • input = "AkjhZ zLKIJz , 23y"
    

    Code

    p input.tr('a-yA-YzZ','b-zB-ZaA')
    

    Output

    "BlkiA aMLJKa , 23z"