Check if char in string?

I have a question:

If you are only testing if a single character is in a string ‘aeiouy’
what are the advantages of using a regular expression instead of
include?
(or index).

If I understand correctly what the original poster is trying to do
(caveat: I’m not at all sure that I do), it is something like:

str = ‘you muppet’
0.upto( str.size - 1 ) do | n |
if ‘aeiouy’.include?( str[ n, 1 ] ) then
puts ‘vowel’
else
puts ‘consonant’
end
end

Regular expressions will work for more complicated tests
(which is why I ought to learn them!),
but do they have an advantage for something like this,
other than getting used to them?

On Thu, May 8, 2008 at 4:10 AM, globalrev [email protected] wrote:

Almost seems that you try to prove Robert’s theory :frowning:
Robert (the other one).

On May 8, 11:27 am, Jim C. <allergic-to-s…@no-spam-
allowed.org> wrote:

if not str[x].chr =~ /[aeiouy]/ then print “YO\n” end

OR

if str[x].chr !~ /[aeiouy]/ then print “YO\n” end

aa = “Hello World”
if(aa.include?(‘o’))
puts ‘yes’
end

ty all for the help. haesob park did what iw anted.

this is the program i wrote, it is an encrypter/decrypter for the
robbers language(rövarspråket in swedish), its from the book Kalle
Blomkvist by late and very famous author Astrid Lindgren.
you add o + the consonant, nothing is done to vowels.
so d becomes dod, a is just a.

dad is therefore dodadod, super is sosupoperor etc.

puts "Enter sentence to encrypt: "
str = gets

enc = “”
for x in (0…str.length()-1)
if str[x].chr =~ /[qwrtpsdfghjklzxcvbnm]/
enc = enc + str[x].chr + “o” + str[x].chr
else
enc = enc +str[x].chr
end
end

print enc

puts "Enter code to decrypt: "
code = gets

x = 0
dec = “”
while (x<code.length())
dec = dec + code[x].chr
if code[x].chr =~ /[qwrtpsdfghjklzxcvbnm]/
x = x + 2
end
x = x + 1
end

print dec

i then changed to !~ just like haesob said but if i do /aeiouy / it
doesnt react to space " ". how do i get it to treat " " as “a”?

Hi,

globalrev wrote:

ty all for the help. haesob park did what iw anted.

this is the program i wrote, it is an encrypter/decrypter for the
robbers language(r�varspr�ket in swedish), its from the book Kalle
Blomkvist by late and very famous author Astrid Lindgren.
you add o + the consonant, nothing is done to vowels.
so d becomes dod, a is just a.

dad is therefore dodadod, super is sosupoperor etc.

puts "Enter sentence to encrypt: "
str = gets

enc = “”
for x in (0…str.length()-1)
if str[x].chr =~ /[qwrtpsdfghjklzxcvbnm]/
enc = enc + str[x].chr + “o” + str[x].chr
else
enc = enc +str[x].chr
end
end

print enc

puts "Enter code to decrypt: "
code = gets

x = 0
dec = “”
while (x<code.length())
dec = dec + code[x].chr
if code[x].chr =~ /[qwrtpsdfghjklzxcvbnm]/
x = x + 2
end
x = x + 1
end

print dec

i then changed to !~ just like haesob said but if i do /aeiouy / it
doesnt react to space " ". how do i get it to treat " " as “a”?
You can do with like this:
if str[x].chr !~ /[\saeiouy]/

BTW, your encryption equals to
str.gsub(/([^\saeiouy])/,"\1o\1")

Regards,
Park H.

On Thu, May 8, 2008 at 8:12 AM, Heesob P. [email protected] wrote:

  enc = enc + str[x].chr + "o" + str[x].chr

code = gets

str.gsub(/([^\saeiouy])/,"\1o\1")
Not quite. [^\saeiouy] is not the same as the set of consonants.

Todd

On 8 Maj, 15:12, Heesob P. [email protected] wrote:

you add o + the consonant, nothing is done to vowels.
enc = enc + str[x].chr + “o” + str[x].chr
x = 0

Park H.

Posted viahttp://www.ruby-forum.com/.

that doesnt seem to work, it repeats the sma ething alot of times
befre next letter.

if this is what u meant(why did u add the “s” ?):
for x in (0…str.length()-1)
str = str.gsub(/([^\aeiouy])/,"\1o\1")
end
print str

On Thu, May 8, 2008 at 11:10 AM, globalrev [email protected] wrote:

Blomkvist by late and very famous author Astrid Lindgren.
if str[x].chr =~ /[qwrtpsdfghjklzxcvbnm]/

print dec
Regards,
end
print str

I’ll repeat. With /[^aeiouy]/ you will get a true for any character
with byte number between 0 and 255 that is not [aeiouy],
including punctuation marks, the capital vowels, and other characters
ala…

“Hello, \n\rtherE!” would become
“HoHelollolo,o,\no\n\ro\rtothoherorEoE!o!”

…and also includes everything above 126 and below 32 which is fine
if that’s what you want. Of course, any key would work, it doesn’t
have to be just consonants, so I guess the point is sort of moot; just
thought I would mention it.

Todd

On 5/8/08, globalrev [email protected] wrote:

On 8 Maj, 15:12, Heesob P. [email protected] wrote:

globalrev wrote:

i then changed to !~ just like haesob said but if i do /aeiouy / it
doesnt react to space " ". how do i get it to treat " " as “a”?

It’s all in the book…

From “Programming Ruby”, in Standard Types/ Regular Expressions
/Character Classes:
" A character class is a set of characters between brackets: …
[aeiou] will match a vowel … addition, you can use the abbreviations
… so that (for example) \s matches any whitespace character"
and a bit further down:
“Put a ^ immediately after the opening bracket to negate a character
class: [^a-z] matches any character that isn’t a lowercase alphabetic”

That should give you all the information to build the expression you
need:
c !~ [\saeiouy]
c
!~ #does not match
/[ #any
\s #whitespace
aeiouy] #or vowel …

which is exactly what Park H. said:

You can do with like this:
if str[x].chr !~ /[\saeiouy]/

An equivalent version moves the ‘not’ inside the character class:
if str[x].chr =~ /[^\saeiouy]/

str = str.gsub(/([^\aeiouy])/,"\1o\1")
end
print str

That’s not what he meant, check the docs for gsub:
“Returns a copy of str with all occurrences of pattern replaced with
either replacement or the value of the block”. So essentially, gsub
does the loop over all the characters for you. Your whole encode
function can be as simple as:

def encode str; str.gsub(/([^\saeiouy])/,"\1o\1"); end

I’d modify it to deal with case:
str.gsub(/([^\saeiouy])/i){|c|"#{c}o#{c.downcase}"}

will encode “Hi Oliver” into
“Hohi Ololivoveror”, instead of
“HoHi OoOlolivoveror”

HTH,
-Adam

Hi,

globalrev wrote:

On 8 Maj, 21:26, Adam S. [email protected] wrote:

[aeiou] will match a vowel … addition, you can use the abbreviations
\s #whitespace

str = str.gsub(/([^\aeiouy])/,"\1o\1")

I’d modify it to deal with case:
str.gsub(/([^\saeiouy])/i){|c|"#{c}o#{c.downcase}"}

will encode “Hi Oliver” into
“Hohi Ololivoveror”, instead of
“HoHi OoOlolivoveror”

HTH,
-Adam

is there some equally clever way to replace decode?

puts "Enter code to decrypt: "
code = gets
x = 0
dec = “”
while (x<code.length())
dec = dec + code[x].chr
if code[x].chr !~ /[\saeiouy]/
x = x + 2
end
x = x + 1
end
print dec
Something like this:
code.gsub(/([^\saeiouy])o([^\saeiouy])/){$1==$2 ? $1 : $& }

Regards,

Park H.

On 8 Maj, 21:26, Adam S. [email protected] wrote:

[aeiou] will match a vowel … addition, you can use the abbreviations
\s #whitespace

str = str.gsub(/([^\aeiouy])/,"\1o\1")

I’d modify it to deal with case:
str.gsub(/([^\saeiouy])/i){|c|"#{c}o#{c.downcase}"}

will encode “Hi Oliver” into
“Hohi Ololivoveror”, instead of
“HoHi OoOlolivoveror”

HTH,
-Adam

is there some equally clever way to replace decode?

puts "Enter code to decrypt: "
code = gets
x = 0
dec = “”
while (x<code.length())
dec = dec + code[x].chr
if code[x].chr !~ /[\saeiouy]/
x = x + 2
end
x = x + 1
end
print dec

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs