Java Program to Check a Given Character Is a Printable Character or Not Without Using the Built-in Library Method

You are currently viewing Java Program to Check a Given Character Is a Printable Character or Not Without Using the Built-in Library Method

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

// 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");
  }
}
// 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");
  }
}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

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply