R
Robert Dober
Almost seems that you try to prove Robert's theory
Robert (the other one).
Robert (the other one).
this way:
if str[x].chr =~ /[aeiouy]/
i can see if a char is any of the chars aeioyubut 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
--
You can do with like this: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,
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 = getsenc = ""
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
endprint encputs "Enter code to decrypt: "
code = getsx = 0
dec = ""
while (x<code.length())
dec = dec + code[x].chr
if code[x].chr =~ /[qwrtpsdfghjklzxcvbnm]/
x = x + 2
end
x = x + 1
endprint deci 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
You can do with like this:
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
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
Something like this:[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
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.