2

I am writing a code to print all characters and its ascii value from 'a' to 'z'.

With the following code snippet I am able to do this.

var c = 'a' while(c < 'z'){ println(c +" = " + c.toInt) var p = c.toInt p += 1 c = p.toChar } 

But when I am doing following(like c)

var c = 'a' while(c < 'z'){ println(c +" = " + c.toInt) c += 1 // or c = c + 1.toChar } 

it is giving me following error

 found : Int required: Char 

Is there any better way to increment character in scala.

Thanks,
Shantanu

6
  • Just a quick question, is there a reason for not doing ('a' to 'z').foreach(println) for your basic task, which is printing ASCII values from a to z? Commented Dec 28, 2013 at 11:55
  • for me ('a' to 'z').foreach(println) is printing a to z only, not ASCII values. Commented Dec 28, 2013 at 11:58
  • 2
    Ohh damn, I misunderstood. You have to add .map(_.toInt) before foreach then. Commented Dec 28, 2013 at 12:00
  • yes. This is nicer way to do this. Thanks. But my main motive for this question is to find out the alternatives for incrementing characters. Commented Dec 28, 2013 at 12:05
  • Hi Patryk, Please add your comment as answer. This seems the best way to do this. Commented Dec 28, 2013 at 12:39

5 Answers 5

7

As requested, here's yet another option moved from comments:

('a' to 'z').map(_.toInt).foreach(println) 
Sign up to request clarification or add additional context in comments.

1 Comment

Whoops. Sorry, didn't read your comments when I posted my answer.
5

In Scala you can make an iterable Range using the expression 'a' to 'z'

('a' to 'z').foreach(println) 

Try it

Comments

2

Another take is to use Streams. Besides being more modular (you can continue conting, map it etc.) it can prove quite efficient to compute things lazily. In this case it's not relevant. But I should be studying for an exam, so I have to spend mandatory slacking-time somwhere...

 val s = Stream.from('a').map(_.toChar) s.take(26).force > Stream(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) 

Or as a single-liner

Stream.from('a').map(_.toChar).take(26).force 

Or with a condition

Stream.from('a').map(_.toChar).takeWhile(_ <= 'z').force 

The example can be seen here: http://www.scalakata.com/52bec5a5e4b0b1a1c4dc0402 (Nice tool btw, thanks @theon!)

Comments

2

This should work:

var c = 'a' while(c < 'z'){ println(c +" = " + c.toInt) c = (c + 1).toChar } 

But it's a hideous use of a var though, I'd advise one of the other solutions.

1 Comment

using var in the example is ugly, but this solution is the one that best addresses the actual question. The original question was about how to increment chars - using "toChar" to convert back to a char is the relevant bit : (c+1) .toChar
0

This is not nearly as awesome as @theon's answer, but here is a for-comprehension version:

scala> for(i <- 'a' to 'z') print(i.toString + ' ') a b c d e f g h i j k l m n o p q r s t u v w x y z 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.