0% found this document useful (0 votes)
21 views

Character Functions

Char variables represent single characters like letters, numbers, and symbols. They can be declared and assigned values like char x='5'. Character functions like isLetter(), isDigit(), and toUpperCase() check properties of and transform char values. For example, isLetter() returns true if the char is a letter, and toUpperCase() returns the uppercase version of a letter char. These functions allow checking and manipulating individual characters in strings using charAt() to extract characters at indexes.

Uploaded by

tanya.a.s.0825
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Character Functions

Char variables represent single characters like letters, numbers, and symbols. They can be declared and assigned values like char x='5'. Character functions like isLetter(), isDigit(), and toUpperCase() check properties of and transform char values. For example, isLetter() returns true if the char is a letter, and toUpperCase() returns the uppercase version of a letter char. These functions allow checking and manipulating individual characters in strings using charAt() to extract characters at indexes.

Uploaded by

tanya.a.s.0825
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Character oriented functions

char variables are letters (A,a) numbers(6) or any special symbols(%) enclosed within
single quotes .

Declare and assign values


char x=’5’;
char x=’A’;
char x=’*’;

Character Type No.of


characters

Capital letters 26

Small case letters 26

Digits 10

Special symbols 32

Graphics characters 128

Control characters 34

Total 256

Input a Character variable


a) Getting input from user

import java.util.*; // import library files


Scanner in = new Scanner(System.in); // Scanner class object
char ch =in.next().charAt(0);

b) Fetch a letter from a string

String str=”HELLO” ;
char ch =s.charAt(0);
char ch1 =s.charAt(1);
char ch2 =s.charAt(2);
Output : H
E
L

// Explanation

charAt(0) returns the string at the zero th position .


charAt(1) returns the string at the zero th position .

Character oriented functions :


Function Return Example (true) False
type

isLetter() boolean
Syntax: Character.isLetter(‘6’);
boolean b=Character.isLetter(character); Character.isLetter(‘*’);
Example
boolean b=Character.isLetter(‘c’);
(Or)
char x=’A’;
boolean b=Character.isLetter(x);

isDigit() boolean
boolean b=Character.isDigit(‘5’); Character.isLetter(‘M’);
Character.isLetter(‘*’);

isLetterOrDigit() boolean boolean b=Character.isLetterOrDigit(‘A’); Character.isLetterOrDigit(‘*’);


boolean b=Character.isLetterOrDigit(‘5’);

isWhitespace() boolean boolean b=Character.isWhitespace(‘ ‘); Character.isLetterOrDigit(‘*’);

isUpperCase() boolean boolean b=Character.isUpperCase(‘A ‘); Character.isUpperCase(‘a‘);

isLowerCase() boolean boolean b=Character.isLowerCase(‘a ‘); Character.isLowerCase(‘A‘);

toUpperCase() char char b=Character.toUpperCase(‘a ‘); Character.toUpperCase(‘A‘);


Output: A
Output : A Character.toUpperCase(‘*‘);
Output: *
Reason : If the input is not in
lowercase / letter , it returns
the same character.

char b=Character.toLowerCase(‘A ‘); Character.toLowerCase(‘a‘);


isLowerCase() char Output : a Output: a
Character.toLowerCase(‘*‘);
Output: *

Reason : If the input is not in


uper case / letter , it returns the
same character.

Combining charAt() with these functions:

a) String s =”WeLCome”;
char m = Character.toUpperCase(s.charAt(2));

Output : L

b) String s =”WeLCome”;
char m = Character.isUpperCase(s.charAt(1));

Output : false

You might also like