Ruby 编程:回文检测与文本换行符转换
1. 回文检测
1.1 简单回文检测思路
字符串可以反转自身,而回文串的定义是其反转后与自身相同。不过,简单的回文检测并不令人完全满意,比如对于一些包含非字母字符的句子就不适用,所以需要更复杂的回文检测方法。
1.2 复杂回文检测实现( palindrome2.rb )
#!/usr/bin/env ruby
# palindrome2.rb
=begin rdoc
Gives every <b>String</b> the ability to identify whether it is a
a palindrome. This version ignores all non-alphabetic characters,
making it suitable for longer text items.
=end
class String
DUAL_CASE_ALPHABET = ('a'..'z').to_a + ('A'..'Z').to_a
=begin rdoc
Contrast this with some other languages, involving iterating through each
string index and comparing with the same index from the opposite end.
Takes 1 optional Boolean, which indicates whether case matters.
Assum
超级会员免费看
订阅专栏 解锁全文
11

被折叠的 条评论
为什么被折叠?



