Write a program to input a string and print out the text with the uppercase and lowercase letters reversed, but all other characters should remain the same as before.(Please use scanner class)
Answers
The program to this question can be given as:
Program:
import java.util.*; //import package for user input.
class ChangeValue//defining class.
{
public static void main(String ar[]) //defining main method
{
int value,j,length; //defining integer variable.
String Val; //defining String variable.
Scanner ob=new Scanner(System.in); //creating Scanner class object.
System.out.println("please input a string value :"); //message.
Val=ob.next(); //taking input.
length=Val.length(); //calculating string length.
for(j=0;j<length;j++) //loop
{
value=Val. charAt(j);//holding value in value variable.
if(value>=65 &&value<=90) //if block
{
value=value+32; //convert value in uppercase.
System.out.print((char)value); //print value.
}
else if(value>=97 &&value<=122) //else-if block
{
value=value-32;//convert value in lower case.
System.out.print((char)value); //print value.
}
else //else block
{
System.out.print((char)value); //print value
}
}
}
}
Output:
please input a string value :
hello
HELLO
Explanation:
- In the above java code firstly import a package for user input then a class "ChangeValue" is defined that contains a main method inside the method an integer variable value,j, and length is defined that is used for change string value into uppercase or lowercase.
- After creating a scanner class object 'ob' in the string variable 'Val' take input from the user and use length variable for calculating the total length of the string. In for loop, a conditional statement is used that converts the value in lowercase or uppercase.
- To convert the value in uppercase if block is used this block converts string value in upper case.
- If the value is inserted in uppercase, so else if block is used that converts the value in lowercase.
- In else part, we print string value.
Learn more:
- change a first latter string in uppercase: https://brainly.in/question/7605973
- how to input string value in java: https://brainly.in/question/8849767
Answer:
Hi there, Here is the answer
import java.io.*;
public class LetterCases
{
public static void main(String args[]) throws IOException
{
int ch;
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE STRING ");:
String input=br.readLine();
int len=input.length();
int i;
for(i=0; i<len; i++)
{
ch=input.charAt(i);
if(ch>=65 && ch<=90)
{
ch=ch+32;
System.out.print((char)ch);
}
else if(ch>=97 && ch<=122)
{
ch=ch-32;
System.out.print((char)ch);
}
else
{
System.out.print((char)ch);
}
}
}
}