Wednesday, January 11, 2012

BlueJ Program on Changing Upper Case Characters To Lower Case Using Recursive Function


In this BlueJ program on string manipulation, the entered text will be modified in such a way that all the upper case characters in the sentence will be converted into lower case characters. Other characters, lower case characters and punctuations will remain unchanged.

Codes of the BlueJ program on string manipulation

import java.io.*;
class Words2
{
  BufferedReader br;
 String text,s1="";
 int len,i;
   public static void main(String args[])throws IOException
   {
  Words2 ob=new Words2();
 ob.accept();
   }
  Words2()
  {
   br=new BufferedReader(new InputStreamReader(System.in));
  }
public void accept()throws IOException
{
 System.out.println("Enter the sentence:");
 text=br.readLine().trim();
 len=text.length();
 i=0;
 result(text,i);
 System.out.println(“Modified string=”+s1);
  }
public void result(String s,int i)
{
if(i==len)
return;
if(s.charAt(i)>=65 && s.charAt(i)<=90)
s1=s1+=(char)(s.charAt(i)+32);
else
s1=s1+s.charAt(i);
i++;
result(s,i);
}
}

Technical Analysis of the BlueJ Program on string manipulation

The function ‘public void accept()’ accepts the sentence from the user and the sentence along with a variable with initial value ‘0’ is passed to another function ‘public void result(String s,int i)’ which is the recursive function. The variable with initial value will access the characters of the sentence one by one within the recursive function. ‘String s1’ is an object of string class which will store the modified value. The characters in the sentence are accessed and checked for upper case and if found necessary changes are done. Simultaneously the string object ‘s1’ is updated and the value of the variable ’i’ is incremented by 1. When the value of ‘i’ becomes equal to the length of the sentence, recursive call is stopped.


Sample input and output of the BlueJ program on string manipulation

Enter the sentence:
This is TEST on Case Changing Program.
this is test on case changing program.

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner