Tuesday, January 27, 2015

Print All Ascii

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

Idea: Recursion. Start from 0, print, add 1 then recursively next until 255

Time: O(1) Space: O(1)

Code: 
 public class printAllASIC {  
      public printAllASIC()  
      {  
           printASIC(0);  
      }  
      public void printASIC(int charNum)  
      {  
           if(charNum<255)  
           {  
                char toPrint=(char)charNum;  
                System.out.println(toPrint);  
                printASIC(charNum+1);  
           }  
      }  
 }  

No comments:

Post a Comment