Computer Science, asked by parthpawarcool24, 5 months ago

Write a program in java to accept a word and display the ascii code of each character of the word.
Sample Input: BLUEJ
Sample Output: B:- 66
L:- 75
U:- 84
E:- 69
J:- 73

Answers

Answered by anindyaadhikari13
16

Required Answer:-

Given Question:

  • Write a program in Java to accept a word and display the ASCII code of each character of the word.

Approach:

This is an easy question. Check out the answer (given below).

import java.util.*;

public class ASCIIValue

{

public static void main(String args[])

{

/*

* Written by: @AnindyaAdhikari.

*/

Scanner sc= new Scanner(System.in);

System.out.print("Enter a word: ");

String word = sc.next();

int len = word.length();

for (int i = 0;i < len; i++)

{

char ch = word.charAt(i);

System.out.println("Character: " + ch);

System.out.println("ASCII Value: " + (int)ch + "\n");

}

sc.close();

}

}

How it's solved ?

Firstly, we will import Scanner class. Scanner class is used to take input from the user. It is present in utility package of Java. In the next step, I have created a class, main() method. Now, I have declared objects of Scanner class. Using scanner class, take a word as input from the user. The next() method of Scanner class is used to input a word from the keyboard. Now, we will calculate the length of the word and then iterate a loop from i=0 to <lengthOfWord> - 1 (Since, index value of a word starts with 0 and ends with length - 1th index). Now, we will extract each characters starting from index 0. We can extract characters from the word by using a method charAt(<IndexValue>). I have stored the character in a variable named ch. Now, we will find the ASCII value of the character by explicit type casting.

Variable Description:

  1. Word (String) :- Used to store the entered word.
  2. Len (int) :- Used to store the length of the entered word.
  3. i (int) :- Loop variable.
  4. ch (char) :- Extract each characters of word and store.

Output is attached.

Attachments:
Answered by BrainlyProgrammer
8

Question:-

  • WAP in Java to accept a word and display the ASCII code of each character of a word.

Sample Input: BLUEJ

SAMPLE OUTPUT:-

B:-66

L:-75

U:-84

E:-69

J:-73

______________________

Answer:

import java.util.*;

class Code

{

public static void main (String ar [])

{

/*

*Solved by

*@swetank232894

*/

Scanner sc=new Scanner (System.in);

System.out.print("Enter a word:-");

String w=sc.next();

char ch;

for(I=0;I<(w.length());I++)

{

ch=w.charAt(I);

System.out.println(ch+":-"+(int)ch);

}

}

}

______________________

Note:-

  1. The "w.length()" returns with the length of the string.
  2. The "charAt(i)" returns with the characters present at i postion.
  3. "(int)ch" means it will find the integer value of that character (ASCII code of that character).
  4. Since the position of the character is 0 so the position of the last character will be n-1(where n is no. of characters).
  5. To input a word we use...sc.next();
  6. To input a line we use ....sc.nextLine();
Similar questions