Home > Software design >  Print char of string uppercase if it is a vowel and lowercase if it is a consonant
Print char of string uppercase if it is a vowel and lowercase if it is a consonant

Time:12-25

I am trying to come up with a verb that will take a string as input and printa character uppercase if it is a vowel i.e. (aeiou) or lowercase if it is a consonant.

s=:'authority'
t=:<&>s
│a│u│t│h│o│r│i│t│y│ NB. boxed s

Expected output is : AUthOrIty I tried writing verb using control flow structure using e.(membership) to test if a char is a vowel and then echo toupper s but that doesn't work.

CodePudding user response:

You don't need to box on individual characters. "0 will apply an atom at a time and in this case your characters are atoms.

I would use e. to check to see if a character is a vowel and if it is apply toupper by using the ^: power conjunction on the test e.&vow

   s=:'authority'
   vow=:'aeiou' NB. define vowels
   
   toupper^:(e.&vow)"0  s 
AUthOrIty

CodePudding user response:

As bob says, there is no reason to box individual characters. I would prefer to use dataflow over control flow here: generate the complete uppercased string, and then select the appropriately-cased characters using }:

   s=:'authority'
   s ,: toupper s
authority
AUTHORITY
   s e.'aeiou'
1 1 0 0 1 0 1 0 0
   (s e.'aeiou')} s ,: toupper s
AUthOrIty

See nuvoc.

A functionally equivalent alternative using {:

   s ,. toupper s
aA
uU
tT
hH
oO
rR
iI
tT
yY
   (s e.'aeiou') {"_1 s ,. toupper s
AUthOrIty
  • Related