【问题标题】:Ruby: making a caesar's cipherRuby:制作凯撒密码
【发布时间】:2016-12-12 01:42:44
【问题描述】:

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

我想要这样的东西:

"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

【问题讨论】:

  • 具体说明您遇到的问题
  • 程序打印的是原始字符串,而不是修改后的字符串。
  • 您只是在更改字符,而不是在原始字符串上替换它。

标签: ruby iteration


【解决方案1】:

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

因为你没有修改字符串。

考虑这段代码:

string = "abcdef"
string.each_char do |character| 
  character = "A"
end
puts string #=> "abcdef"

如您所见,仅将变量character 设置为"A" 并不会替换原字符串中的相应字符

现在考虑一下:

string = "abcdef"
string = string.gsub(/./) do |character| 
  character.succ
end
puts string #=> "bcdefg"

这应该会给你一些想法。

【讨论】:

  • 好的,所以我更改了 character.sub!进入 string.sub!替换原始字符串,但它只适用于字母。偏移量 1,例如“abc”>>“bcd”“hello 123”>>“jgnnq 543”
  • 我不打算为您编写代码。我在你,展示如何改变每个角色的一般策略。您将其更改为什么取决于您。
【解决方案2】:

您只是在更改字符,而不是在原始字符串上替换它。您可以创建一个 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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 2020-05-31
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    相关资源
    最近更新 更多