Check if char in string?

C

Colin Bartlett

[Note: parts of this message were removed to make it a legal post.]

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?
 
A

Ashutosh

this way:
if str[x].chr =~ /[aeiouy]/
i can see if a char is any of the chars aeioyu
but i want to see if its not...
if str[x].chr !=~ /[aeiouy]/

how about something like:

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
 
G

globalrev

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"?
 
H

Heesob Park

Hi,
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 Heesob
 
G

globalrev

Hi,


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 Heesob

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
 
A

Adam Shelly

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 Heesob 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]/
BTW, your encryption equals to
str.gsub(/([^\saeiouy])/,"\\1o\\1")

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

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
 
G

globalrev

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 Heesob 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]/


BTW, your encryption equals to
str.gsub(/([^\saeiouy])/,"\\1o\\1")
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

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

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
 
H

Heesob Park

Hi,
[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 Heesob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,065
Members
47,672
Latest member
svaraho

Latest Threads

Top