Saturday, January 11, 2014

Toggle Cases Of Each Letter Of A Sentence Using BlueJ


In this program a sentence will be entered by user and case of each letters of the sentence will be changes – upper case characters will be converted into lower case and vice versa.

Here is the BlueJ program

import java.io.*;

class CaseSentence
{
String s1,s2="";
char ch;
int i,len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeString()throws Exception
{
System.out.println("Enter any sentence:");
s1=br.readLine();
len=s1.length();
for(i=0;i< len;i++)
{
ch=s1.charAt(i);
if(Character.isLetter(ch))
{
 if(Character.isUpperCase(ch))
 ch=Character.toLowerCase(ch);
 else
ch=Character.toUpperCase(ch);
}
s2=s2+ch;
}
System.out.println("Original sentence="+s1);
System.out.println("Modified sentence="+s2);
}
public static void main(String args[]) throws Exception
{
CaseSentence ob=new CaseSentence ();
ob.takeString();
}
}

Description of the toggle case program


Here each characters of the sentence are extracted using charAt() function and then a number of Character class static functions are used to complete the job. Firstly the extracted character is checked whether it is letter or not using isLetter () function. If the character is a letter, case of the letter is checked by using isLowerCase () function and accordingly case of the letter is changed using toUpperCase () or toLowerCase() function.


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner