biswasrohit20

f# Ex 3.1

May 16th, 2021 (edited)
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.18 KB | None | 0 0
  1. // Exercise 3.1 – Pattern matching and recursion
  2. // a. Define a function vowelToUpper c that converts the characters a, e, i, o
  3. // and u to capitals (upper case). All other characters should be returned unchanged.
  4. // You are not allowed to use the standard F# method.ToUpper() on strings!
  5.  
  6.  
  7.  
  8.  
  9. open System
  10.  
  11. let vowelToUpper (c:char):char=
  12.     if (c = 'a') then 'A'
  13.     else if (c = 'e') then 'E'
  14.     else if (c = 'i') then 'I'
  15.     else if (c = 'o') then 'O'
  16.     else if (c = 'u') then 'U'
  17.     else c
  18.    
  19.    
  20. printfn "%c" (vowelToUpper('e'))
  21.  
  22. Console.ReadKey() |> ignore
  23.  
  24.  
  25.  
  26.  
  27. // b. Define another function (choose your own function name) to convert all
  28. // occurrences of a, e, i, o, and u in a string to capitals. Write the function to use
  29. // recursion and the vowelToUpper in 3.1a above
  30.  
  31.  
  32.  
  33. open System
  34.  
  35. let vowelToUpper (c:char):char=
  36.     if (c = 'a') then 'A'
  37.     else if (c = 'e') then 'E'
  38.     else if (c = 'i') then 'I'
  39.     else if (c = 'o') then 'O'
  40.     else if (c = 'u') then 'U'
  41.     else c
  42.    
  43. let allVowel(s:string) = String.collect (fun c -> sprintf"%c" (vowelToUpper(c))) s
  44.  
  45. printfn "%s" (allVowel("rohit biswas"))
  46. Console.ReadKey() |> ignore
Add Comment
Please, Sign In to add comment