arraysstringrecursionf#toupper

F# change a letter in a string


I need to finish a method in F#. All-day I was searching for some methods to change a letter in a string and did not found anything. I have done the (a) and now I need to use the method from (a) in (b).

The task

let rec vowelToUpper x : char = 
    match x with 
    | 'a' -> 'A'
    | 'e' -> 'E'
    | 'i' -> 'I'
    | 'o' -> 'O'
    | 'u' -> 'U'
    | _ -> x
let rec converter (str ) length =  
    if length=0 then 
        str
    else
        str.[length-1] <- vowelToUpper str.[length-1]
        converter str (length-1)
  

Neither <- nor = works, in this case, the error says to consider using some constraints. I think I need to use the list of chars constraint on the str. Also, maybe it is because it's not mutable? And since this is a basic thing to do in any programming language, why is it that hard to find the solution on internet?)


Solution

  • This worked out for me by generating a new string.

    
    let rec converter (str : string) (finalStr : string) length =  
        if length=0 then 
            finalStr
        else
            converter str (finalStr + (vowelToUpper str.[length-1]).ToString()) (length-1)