Write a function called isLowerCase that takes as a parameter a char and returns a boolean indicating whether the char is a lower-case (ASCII) letter.
Do not call one of the JDK's library functions, like Character.isLowerCase; instead, implement the function from scratch.
I just need a basic function using the most basic methods.
import java.util.*;
import java.util.Scanner;
public class MyClass {
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter a character: ");
char c = sc.next().charAt(0);
isLowerCase(c);
}
public static void isLowerCase(char c)
{
if(c == ' ' || c == '\t' || c == '\r' || c == '\n')
{
System.out.println("enter valid character");
}
else
{
if (c >= 'a' && c <= 'z')
{
System.out.println("The character entered is lowercase");
}
else
{
System.out.println("The character entered is not in
lowercase");
}
}
}
}
Explanation:
import java.util.*; //This package includes all the
libraries
import java.util.Scanner; //This package inludes library for
Scanner class which helps to read input from user.
public class MyClass {
public static void main(String args[])
{
Scanner sc= new Scanner(System.in); // Scanner class to read the
user input
System.out.print("Enter a character: ");
char c = sc.next().charAt(0); //This allows to compiler to read
only one character that is at the index 0 even if user enters more
then one character.
isLowerCase(c); //call to the isLowerCase (c) method by passing
user inputted character as parameter
}
public static void isLowerCase(char c) //method to check if
character is lowercase
{
if(c == ' ' || c == '\t' || c=='\r' || c == '\n')
//this makes the code to print enter valid character if
user enters an empty space , tab, carriage return or a line
{
System.out.println("enter valid character");
}
else
{
if (c >= 'a' && c <= 'z') // this if condition
compares the entered character on the basis of ascii value
{
System.out.println("The character entered is lowercase");
}
else
{
System.out.println("The character entered is not in
lowercase");
}
}
}
}
Snapshot of the output obtained is shown below(I used an online compiler):
case1: When user enters character with first character as lowercase character
case2: When user enters character with first character as uppercase character
Get Answers For Free
Most questions answered within 1 hours.