>
Click HERE for the Methods of Character class.
The ASCII Code: (Part 2)
- Printing the ASCII code for a character:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// ASCII code demo import java.util.Scanner; class test { public static void main(String ar[]) { Scanner scan = new Scanner(System.in); System.out.println("Enter the character to check the ASCII code:"); char ch = scan.next().charAt(0); // input of character // We know that the (Decimal) integer value of a character is its ASCII value. // Hence, just coverting the character to integer will do the triick. int value = (int)ch; System.out.println("Ascii code for: "+ch+" is "+value ); } } /* OUTPUT: Enter the character to check the ASCII code: a Ascii code for: a is 97 Enter the character to check the ASCII code: A Ascii code for: A is 65 */ - Printing the character based on the ASCII code input (Reverse of the previous program):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// ASCII code demo import java.util.Scanner; class test { public static void main(String ar[]) { Scanner scan = new Scanner(System.in); System.out.println("Enter the integer ASCII code:"); int value = scan.nextInt(); // input of Decimal value // Just covert the integer to character. char ch = (char)value; System.out.println("Character value for ASCII code: "+value+" is "+ch ); } } /* OUTPUT Enter the integer ASCII code: 67 Character value for ASCII code: 67 is C Enter the integer ASCII code: 99 Character value for ASCII code: 99 is c */ - Printing the ASCII code every character in an input String:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Print the ASCII values for all the characters present in the input string entered by user import java.util.Scanner; class test { public static void main(String ar[]) { Scanner scan = new Scanner(System.in); System.out.println("Enter the string:"); String str = scan.next();// input a string. // As string is combination of different characters, we need to extrac // individual character and then print the ASCII respectivly. for(int i=0;i<str.length();i++) { char ch = str.charAt(i); // extract individual characters by charAt method int value = (int)ch; System.out.println(ch+" ASCII code is "+value); } } } /* OUTPUT Enter the string: India I ASCII code is 73 n ASCII code is 110 d ASCII code is 100 i ASCII code is 105 a ASCII code is 97 */
Click HERE for the Methods of Character class.
Comments
Post a Comment