Page 260 - Cs_withBlue_J_C11_Flipbook
P. 260

10.1 CHARACTER CLASS
              The Character is a built-in class in Java that provides various methods to manipulate characters. An object of type
              character contains a single value of char type. The syntax to create a Character class’s object with the Character()
              constructor is as follows:

                  Character ch = new Character('p');
              The above statement creates a Character class’s object which contains ‘p’ of type char. Like String class, the object of
              Character class once created, cannot be changed.

              10.1.1 Methods of Character Class
              The methods of Character class are as follows:
              •  Character.isLetter(char): This function checks whether the given character is a letter or not and returns true if it is a
                letter.
              •  Character.isDigit(char): This function checks whether the given character is a digit or not and returns true if it is a
                digit.
              •  Character.isUpperCase(char): This function returns true if the alphabet is in capital letters.
              •  Character.isLowerCase(char): This function returns true if the alphabet is in small letters.
              •  Character.toLowerCase(char): This function converts the character to lowercase.
              •  Character.toUpperCase(char): This function converts the character to uppercase.
              •  Character.isLetterOrDIgit(char): This function checks and returns true or false depending on the character in the
                parameter. If it is an alphabet or a digit, it will return true, else false.
              •  Character.isWhitespace(char): This function returns true, if the character in the parameter is a white space, else it
                will return false.
              Some examples related to the above methods are tabulated below:

                        Method                         Example                 Result             Explanation
               Character.isLetter(char)   boolean B=Character.isLetter(‘a’)  true       ‘a’ is an alphabet
               Character.isDigit(char)    boolean B= Character.isDigit(‘1’)  true       ‘1’ is a digit
               Character.isUpperCase(char)  boolean B= Character.isUpperCase(‘a’)  false  ‘a’ is in lowercase
               Character.isLowerCase(char)  boolean B= Character.isLowerCase(‘a’)  true  ‘a’ is in lowercase
               Character.toUpperCase(char)  char c= Character.toUpperCase(‘d’)  D       Converts ‘d’ to ‘D’
               Character.toLowerCase(char)  char c= Character.toLowerCase(‘d’)  d       Remains the  same  because ‘d’  is in
                                                                                        lower case
               Character.isLetterOrDigit(char)  boolean b= Character.isLetterOrDigit(‘4’) true  ‘4’ is a digit
               Character.isWhitespace(char)  boolean b= Character.isWhitespace(‘u’)  false  ‘u’ is not a white space



                Program 1      Write a program in Java to input a sentence and count the number of words, alphabets and
                               digits in the sentence.

                1       import java.util.*;

                2       class string_count
                3       {

                4           public static void main()
                5           {




                258258  Touchpad Computer Science-XI
   255   256   257   258   259   260   261   262   263   264   265