Write a java program to accept a string and convert each char from upper to lower and lower to upper. Example => i/p -> WELcome o/p -> welCOME

import java.util.*;
class StringConvert
{
String str;
        //To accept string 
StringConvert()
{
Scanner s=new Scanner(System.in);
System.out.print("Enter string : ");
str=s.next(); //  next() method finds and returns the next complete token from this scanner.
}
        //To convert chars upper to lower and vice versa 
void convert()
{
for(int i=0;i<str.length();i++)
{
char p=str.charAt(i);
if(p>65  && p<96) // 65 is Ascii value of Capital letter A and 96 Capital letter Z
{
int e=p+32;
char f=(char)e;  // To convert int to char
System.out.print(f);
}
if(p>96 && p<129) //97 is Ascii value of small letter a and 129 small letter z
{
int e=p-32;
char f=(char)e; // To convert int to char
System.out.print(f);
}
}
}
}
//To create object of above class
class TestStringConvert
{
static public void main(String args[])
{
StringConvert obj=new StringConvert();
obj.convert();
}
}

0 Comments