Wednesday, January 12, 2011

BlueJ Program On Changing The Case Of Characters In String



This program deals with changing the case of the characters of an entered string. Upper case characters will be changed to lower case characters and vice versa. We can do this program using different techniques.

 How to proceed on this character case changing program

We have to extract each characters of the string. To perform this job, a loop is required and then the status of character is to be checked. This checking can be performed in different ways. We can use static functions of Charater class to check status of any character, whether it is in lower case or upper case. The second approach of checking is ASCII values of each character. Either of these techniques can be used to check the case of the characters and required changes can be done.

Here is the program which checks the character case using static function

import java.io.*;
class ChangeCase
{
String str1,str2="";
char ch;
int i,len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 public void show()throws Exception
{
 System.out.println("Enter the sentence:");
 str1=br.readLine();
  len=str1.length();
 for(i=0;i< len;i++)
{
ch=str1.charAt(i);
if(Character.isUpperCase(ch))
ch=(char)(ch+32);
else if(Character.isLowerCase(ch))
ch=(char)(ch-32);
str2=str2+ch;
}
System.out.println("Modified string="+str2);
}
public static void main(String args[])throws Exception
{
 new ChangeCase ().show();
}
}

Two unfamiliar functions are used in this program, ‘isUpperCase()’ and ‘isLowerCase()’. These are static functions of Character class. Character is a predefined wrapper class of java.lang package. Prototype of these two functions are ‘public boolean isUpperCase(char)’ and ‘public boolean isLowerCase(char)’. If the argument char value in ‘public boolean isUpperCase(char)’ is in upper case it return true otherwise false. The other function is just the reversed.

Here is the second program which checks the character case using ASCII values

import java.io.*;
class Pattern
{
String str1,str2="";
char ch;
int i,len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 public void show()throws Exception
{
 System.out.println("Enter the sentence:");
 str1=br.readLine();
  len=str1.length();
 for(i=0;i< len;i++)
{
ch=str1.charAt(i);
if(ch>=65 && ch<=90)
ch=(char)(ch+32);
else if(ch>=97 && ch< =122)
ch=(char)(ch-32);
str2=str2+ch;
}
System.out.println("Modified string="+str2);
}
public static void main(String args[])throws Exception
{
 new Pattern().show();
}

1 comment:

  1. Your code is little bit wrong
    {
    ch=str1.charAt(i);
    if(ch>=65 && ch<=90)
    ch=(char)(ch+32);
    else if(ch>=97 && ch< =122)
    ch=(char)(ch-32);
    str2=str2+ch;
    }



    this is the right one in which there is no space between< and =

    {
    ch=str1.charAt(i);
    if(ch>=65 && ch<=90)
    ch=(char)(ch+32);
    else if(ch>=97 && ch<=122)
    ch=(char)(ch-32);
    str2=str2+ch;
    }

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner