Computer Science, asked by harshitpandey431868, 5 months ago

give answer if you know
write a program in java to input a character.Display next 5 characters​

Answers

Answered by cornermovie31
0

Answer:

Next 5 Characters - Java

We can use Scanner to take user input. We will be considering the ASCII Table for characters.

The ASCII Table has codes ranging from 0-127 (total 128). The characters corresponding to 0-31 are Non-Printable. 32-127 are Printable Characters.

We will be only considering the Printable Characters.

We can typecast a char into int or byte to get the ASCII Code. We can then just increase this integer value by 1 and typecast back into char to get the corresponding character.

Note that we must use Modulo (%) 128 operation, because ASCII ranges from 0-127. Also, if at some moment our integer lies in 0-31, we must add 32 to it and bring it to the Printable Character Range.

Here's a Java Program which does everything mentioned and prints the next 5 characters from the input character.

\rule{320}{1}

import java.util.Scanner;

public class NextFiveChar

{

public static void main(String[] args)

{

Scanner scr = new Scanner(System.in);

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

char inputCharacter = scr.next(). charAt(0); //Character Input

scr.close();

System.out.println("The next 5 printable ASCII Characters are:");

int ASCII_Code = (int)inputCharacter; //Typecast char into int

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

{

ASCII_Code = (ASCII_Code + 1) % 128; //ASCII Codes range from 0 to 127

if(ASCII_Code < 32) //0-31 are Non-Printable Characters

{

ASCII_Code += 32;

}

//Typecast into char and print

System.out.println((char)ASCII_Code);

}

}

}

Similar questions