Ruby:制作一个caesar的密码

时间:2016-12-12 01:42:44

标签: ruby iteration

所以我试图遍历一个字符串,并根据偏移量替换每个字母数字字符。

我想要这样的事情:

" abc 123!@#$%"偏移

1:" bcd 234!@#$%"

2:" cde 345!@#$%"

我的代码存在的问题是它不会修改字符串。

alphabets = [*?a..?z] #makes an array of all the alphabets
    digits = [*?0..?9] #makes an array of all the single digits

    puts "offset?"
    offset = gets.chomp.to_i
    puts "string?"
    string = gets.chomp

    string.each_char do |character|

        if character[/[a-zA-Z]/] == character #checks if the character is an alphabet
            char_index = alphabets.index(character) + offset #gets the index of the current character being iterated and adds the offset
            #if the (index + offset) % 26 > 0, that means that the index is beyond 25. 
            #Then it will find the remainder and apply that as the new index
            char_index = char_index % 26 if char_index % 26 >= 0 
            character.sub!(character, alphabets[char_index]) #replaces the character with the offset character 
        elsif character[/\d/] == character #checks if the character is a number
            char_index = digits.index(character) + offset 
            char_index = char_index % 10 if char_index % 10 >= 0
            character.sub!(character, digits[char_index])
        end
        #if the character is neither an alphabet nor a number, nothing will run for that character
    end

    puts string

2 个答案:

答案 0 :(得分:3)

  

我的代码存在的问题是它不会修改字符串。

因为没有修改字符串。

考虑以下代码:

character

如您所见,仅将变量"A"设置为string = "abcdef" string = string.gsub(/./) do |character| character.succ end puts string #=> "bcdefg" 并不会替换原始字符串中的相应字符

现在考虑一下:

mmaped

那应该给你一些想法。

答案 1 :(得分:1)

您只是更改了角色,但没有在原始字符串上替换它。您可以创建 modifiedString 并在每次修改后附加相应的字符(或不是):

modifiedString << character

代码:

alphabets = [*?a..?z] #makes an array of all the alphabets
    digits = [*?0..?9] #makes an array of all the single digits

    puts "offset?"
    offset = gets.chomp.to_i
    puts "string?"
    string = gets.chomp
    modifiedString = ""
    string.each_char do |character|

        if character[/[a-zA-Z]/] == character #checks if the character is an alphabet
            char_index = alphabets.index(character) + offset #gets the index of the current character being iterated and adds the offset
            #if the (index + offset) % 26 > 0, that means that the index is beyond 25. 
            #Then it will find the remainder and apply that as the new index
            char_index = char_index % 26 if char_index % 26 >= 0 
            puts"#{character} + #{alphabets[char_index]}"
            character.sub!(character, alphabets[char_index]) #replaces the character with the offset character 
            modifiedString << character
        elsif character[/\d/] == character #checks if the character is a number
            char_index = digits.index(character) + offset 
            char_index = char_index % 10 if char_index % 10 >= 0
            puts"#{character} + #{digits[char_index]}"
            character.sub!(character, digits[char_index])
            modifiedString << character
        else
                    modifiedString << character
        end
        #if the character is neither an alphabet nor a number, nothing will run for that character

    end
    puts "Original String: " + string
    puts "Modified One: " + modifiedString

注意:我还会打印字符+ modifiedCharacter来检查发生了什么。

注2:提一下,如果没有修改字符,你仍然需要将它附加到modifiedString。

IO示例:

offset?
2
string?
asdasf242t%&%$&$%&23ad
a + c
s + u
d + f
a + c
s + u
f + h
2 + 4
4 + 6
2 + 4
t + v
2 + 4
3 + 5
a + c
d + f
Original String: asdasf242t%&%$&$%&23ad
Modified One: cufcuh464v%&%$&$%&45cf

希望它有所帮助。