AdSense

Sunday, February 22, 2015

Print ASCII


Warm-up question: Write a function that prints all ASCII characters. You are not allowed to use for/while loop

Always starts from basic contents from Programming 101 that hibernates deep inside my memory.-_-

ASCII, abbreviated from American Standard Code for Information Interchange, is a character-encoding scheme, which encodes 128 specified characters into 7-bit binary integers.

In Java, we can convert the integer (0 - 127) to the corresponding ASCII character.


public class PrintASCII {
 public static void printASCII() {
  printASCII(0);
 }
 private static void printASCII(int x) {
  char y = (char) x;
  System.out.println(x++ + ": " + y);
  if (x < 128)
   printASCII(x);
 }

 public static void main(String[] args) {
  printASCII();
 }
}

No comments:

Post a Comment