-1

I want to write a program which should take a string and replace each letter in a string with next one in alphabetical order. Then it should convert letters a, e, i, o, u if they exist in a string to upper case. For example, if the string is "How are you", then it should convert it into "Ipx bsf zpv". I have tried this but I am stuck. In addition, I think there should be a better way instead of typing so many if else in my program.

object Foo {
  def main(args: Array[String]) {
    val a: String = "How are you"
    val d:String = ""
    var l = a.length
    var i:Int = 0
    while(l != 0){
      while(i != a.length) {
        if (a(i) == 'a')
        d charAt(_) = 'b'
        else if (a(i) == 'b')
        d charAt(_) = 'c'
        else if (a(i) == 'c')
        d charAt(_) = 'd'
        else if (a(i) == 'd')
        d charAt(_) = 'e'
        else if (a(i) == 'e')
        d charAt(_) = 'f'
        else if (a(i) == 'f')
        d charAt(_) = 'g'
        else if (a(i) == 'g')
        d charAt(_) = 'h'
        else if (a(i) == 'h')
        d charAt(_) = '_'
        else if (a(i) == '_')
        d charAt(_) = 'j'
        else if (a(i) == 'j')
        d charAt(_) = 'k'
        else if (a(i) == 'k')
        d charAt(_) = 'l'
        else if (a(i) == 'l')
          d charAt(_) = 'm'
        else if (a(i) == 'm')
          d charAt(_) = 'n'
        else if (a(i) == 'n')
          d charAt(_) = 'o'
        else if (a(i) == 'o')
          d charAt(_) = 'p'
        else if (a(i) == 'p')
          d charAt(_) = 'q'
        else if (a(i) == 'q')
          d charAt(_) = 'r'
        else if (a(i) == 'r')
          d charAt(_) = 's'
        else if (a(i) == 's')
          d charAt(_) = 't'
        else if (a(i) == 't')
          d charAt(_) = 'u'
        else if (a(i) == 'u')
          d charAt(_) = 'v'
        else if (a(i) == 'v')
          d charAt(_) = 'w'
        else if (a(i) == 'w')
          d charAt(_) = 'x'
        else if (a(i) == 'x')
          d charAt(_) = 'y'
        else if (a(i) == 'y')
          d charAt (_) = 'z'

      }
        i = i + 1
        l = l - 1
      var acc:String = acc + d(0)
      println(acc)
    }
  }
  }
adit
  • 95
  • 2
  • 10
  • Did you try to google your question? https://www.google.com.ua/search?q=replace+letter+next+letter – Dmytro Mitin Oct 15 '17 at 20:23
  • 1
    What about z. Should it rotate to a? This is similar in most languages, just increment byte values of characters in loop. – Piro Oct 16 '17 at 04:36
  • Possible duplicate of [Letter → next letter and capitalize vowels](https://stackoverflow.com/questions/21296270/letter-%e2%86%92-next-letter-and-capitalize-vowels) – Dmytro Mitin Oct 16 '17 at 10:18

2 Answers2

1

Use collect and transform each char of the string.

val vowels = Set('a', 'e', 'i', 'o', 'u')

str.collect {
 case x if x == ' ' => ' '
 case x if vowels(x) => x.toUpper
 case x => (x.toByte + 1).toChar
}
Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40
1

you can use a map function as below

val str = "how are you"

val transformedStr = str.map(x => x match {
  case ' ' => ' '
  case y => {
    val convertedChar = (y.toByte+1).toChar
    if(Array('a', 'e', 'i', 'o', 'u').contains(convertedChar)) convertedChar.toUpper
    else convertedChar
  }
})

println(transformedStr)

the final converted string should be

Ipx bsf zpv
Ramesh Maharjan
  • 41,071
  • 6
  • 69
  • 97