Sunteți pe pagina 1din 3

Class Change

1/2

package Recursion;
/**
* PROGRAM TO CHANGE THE CASE OF A STRING
*
*/
import java.io.*;
public class Change
{
String str;
//data members
String newstr; //data members
int len;
//data members
DataInputStream d=new DataInputStream(System.in);
Change()
{
str="";
newstr="";
len=0;
}

//default constructor

//method to input the word


void inputword()throws IOException
{
System.out.println("ENTER A STRING");
str=d.readLine();
}
//function to convert case of a character
char caseconvert(char ch)
{
int k=' ';
if(ch!=' ')
{
if(ch>92)
{
k=ch-32;//case conversion to capital
}
else
{
k=ch+32;//case conversion to small
}
}
return (char)k; //returning character of opp. case
}
//recursive function
void recchange(int n)
{
Jan 19, 2015 11:16:52 PM

Class Change (continued)

2/2

if(n<(str.length()))
{
newstr=newstr+caseconvert(str.charAt(n));
//extracting a character and changing its case simultaneously
recchange(n+1);
//recursive technique
}
}
void display()
{
System.out.println("ORIGINAL WORD
System.out.println("CHANGED WORD
//displaying output
}

"+str);
"+newstr);

void main()throws IOException


{
Change obj1=new Change();
//creating object
obj1.inputword();
//calling method to execute program
obj1.recchange(0);
//calling method to execute program
obj1.display();
//calling method to execute program
}
//end of main
}

Jan 19, 2015 11:16:52 PM

change1.main();
ENTER A STRING
ToDAy iS THursDAy
ORIGINAL WORD
ToDAy iS THursDAy
CHANGED WORD

tOdaY Is thURSdaY

change1.main();
ENTER A STRING
THe tAj maHal iS in AgRA
ORIGINAL WORD
THe tAj maHal iS in AgRA
CHANGED WORD

thE TaJ MAhAL Is IN aGra

S-ar putea să vă placă și