ADVERTISEMENT
ADVERTISEMENT

Java program to check a given character is a printable character or not without using the built-in library method

Given a character, we have to check whether it is a printable character or not without using the built-in library method.
Submitted by Nidhi, on February 25, 2022

Problem Solution:

In this program, we will create three character variables and check they contain a printable character or not, and print the appropriate message.

Program/Source Code:

The source code to check a given character is a printable character or not without using the built-in library method is given below. The given program is compiled and executed successfully.

// Java program to check a given character is a printable character 
// or not without using the built-in library method

public class Main {

  static boolean isPunctuation(char ch) {
    if (ch == '!' || ch == '\"' || ch == '#' || ch == '$' || ch == '%' || ch == '&' || ch == '\'' || ch == '(' || ch == ')' || ch == '*' || ch == '+' || ch == ',' || ch == '-' || ch == '.' || ch == '/' || ch == ':' || ch == ';' || ch == '<' || ch == '=' || ch == '>' || ch == '?' || ch == '@' || ch == '[' || ch == '\\' || ch == ']' || ch == '^' || ch == '`' || ch == '{' || ch == '|' || ch == '}')
      return true;
    return false;
  }

  static boolean isAlphaNumeric(char ch) {
    if ((ch >= '0' & ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
      return true;
    return false;
  }

  static boolean isPrintable(char ch) {
    if (isAlphaNumeric(ch) || isPunctuation(ch))
      return true;
    return false;
  }

  public static void main(String[] args) {
    char ch1 = 'a';
    char ch2 = 'A';
    char ch3 = 95;

    if (isPrintable(ch1))
      System.out.printf("Given character is a printable character\n");
    else
      System.out.printf("Given character is not a printable character\n");

    if (isPrintable(ch2))
      System.out.printf("Given character is a printable character\n");
    else
      System.out.printf("Given character is not a printable character\n");

    if (isPrintable(ch3))
      System.out.printf("Given character is a printable character\n");
    else
      System.out.printf("Given character is not a printable character\n");
  }
}

Output:

Given character is a printable character
Given character is a printable character
Given character is not a printable character

Explanation:

In the above program, we created a public class Main. It contains four static methods main(), isPunctuation(), isAlphanumeric(), and isPrintable().

The isPunctuation() method returns a Boolean value based on input character, it returns true if the given character is a punctuation mark otherwise it returns false.

The isAlphanumeric() method returns a Boolean value based on input character, it returns true if a given character is an alphanumeric character otherwise it returns false.

The isPrintable() method returns a Boolean value based on input character, it returns true if a given character is a printable character otherwise it returns false.

The main() method is an entry point for the program. Here, we created three variables with some initial values. Then we checked variables containing a printable character or not and printed appropriate messages.

Java Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.