ADVERTISEMENT
ADVERTISEMENT

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

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

Problem Solution:

In this program, we will read a character from the user and check the input character is a punctuation mark or not without using any built-in library method.

Program/Source Code:

The source code to check a given character is a punctuation mark 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 punctuation mark 
// or not without using the built-in library method

import java.util.Scanner;

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;
  }

  public static void main(String[] args) {
    Scanner X = new Scanner(System.in);

    char ch = 0;

    System.out.printf("Enter character: ");
    ch = X.next().charAt(0);

    if (isPunctuation(ch))
      System.out.printf("Given character is a punctuation mark\n");
    else
      System.out.printf("Given character is not a punctuation mark\n");
  }
}

Output:

RUN 1:
Enter character: ?
Given character is a punctuation mark

RUN 2:
Enter character: R
Given character is not a punctuation mark

Explanation:

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains two static methods main() and isPunctuation().

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 main() method is an entry point for the program. Here, we read a character from the user and checked input character is a punctuation mark or not and printed the appropriate message.

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.