Advertisements
Advertisements
प्रश्न
Write a program using a class with the following specifications:
Class name: Caseconvert
Data members: String str
Member functions:
void getstr(): to accept a string
void convert(): to obtain a string after converting each uppercase letter into lowercase and vice versa.
void display(): to print the converted string.
कोड लेखन
उत्तर
import java.util.*;
public class Caseconvert
{String str = "";
void getstr()
{Scanner ob = new Scanner(System.in);
System.out.println("Enter the string");
str = ob.nextLine();
}
void convert()
{char ch;
String t = "";
for (int i = 0; i < str.length(); i++)
{ch = str.charAt(i);
if(Character.isLetter(ch))
{
if(Character.isUpperCase(ch))
t += Character.toLowerCase(ch);
else
t += Character.toUpperCase(ch);
}}
str = t;
}
void display()
{System.out.println(str);
}
public static void main(String args[])
{Caseconvertob = new Caseconvert();
ob.getstr();
ob.convert();
ob.display();
}}
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?