Computer Applications

Write a program using a class with the following specifications:

Class name: Caseconvert

Data MembersPurpose
String strTo store the string
Member MethodsPurpose
void getstr()to accept a string
void convert()to obtain a string after converting each upper case letter into lower case and vice versa
void display()to print the converted string

Java

Java Classes

23 Likes

Answer

import java.util.Scanner;

public class Caseconvert
{
    private String str;
    private String convStr;
    
    public void getstr() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the string: ");
        str = in.nextLine();
    }
    
    public void convert() {
        char arr[] = new char[str.length()];
        for (int i = 0; i < str.length(); i++) {
            if (Character.isUpperCase(str.charAt(i)))
                arr[i] = Character.toLowerCase(str.charAt(i));
            else if (Character.isLowerCase(str.charAt(i)))
                arr[i] = Character.toUpperCase(str.charAt(i));
            else
                arr[i] = str.charAt(i);
        }
        convStr = new String(arr);
    }
    
    public void display() {
        System.out.println("Converted String:");
        System.out.println(convStr);
    }
    
    public static void main(String args[]) {
        Caseconvert obj = new Caseconvert();
        obj.getstr();
        obj.convert();
        obj.display();
    }
}

Variable Description Table

Program Explanation

Output

Answered By

13 Likes


Related Questions