JAVA-6

Q.6 Write a java program to design a class Mystring having data member of type String add member
function to achieve following task.

Solution :- 

import java.util.Scanner;

class MyString {
    private String str;

    public MyString(String str) {
        this.str = str;
    }

    // Method to reverse the string
    public String reverseString() {
        return new StringBuilder(str).reverse().toString();
    }

    // Method to convert string to tOGGLE cASE
    public String toggleCase() {
        StringBuilder toggled = new StringBuilder();
        for (char ch : str.toCharArray()) {
            if (Character.isUpperCase(ch)) {
                toggled.append(Character.toLowerCase(ch));
            } else {
                toggled.append(Character.toUpperCase(ch));
            }
        }
        return toggled.toString();
    }

    // Method to convert string to Sentence case
    public String sentenceCase() {
        if (str.isEmpty()) return str;
        return Character.toUpperCase(str.charAt(0)) + str.substring(1).toLowerCase();
    }

    // Method to extract N characters from right-end of the string
    public String extractRight(int n) throws Exception {
        if (n > str.length() || n < 0) {
            throw new Exception("Invalid length for extraction");
        }
        return str.substring(str.length() - n);
    }
}

public class MyStringOperations {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter a string: ");
        String inputString = scanner.nextLine();
        MyString myString = new MyString(inputString);
        
        while (true) {
            System.out.println("\nChoose an operation:");
            System.out.println("1. Reverse String");
            System.out.println("2. Toggle Case");
            System.out.println("3. Sentence Case");
            System.out.println("4. Extract N characters from right");
            System.out.println("5. Exit");
            System.out.print("Enter your choice: ");
            
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline
            
            switch (choice) {
                case 1:
                    System.out.println("Reversed String: " + myString.reverseString());
                    break;
                case 2:
                    System.out.println("Toggle Case String: " + myString.toggleCase());
                    break;
                case 3:
                    System.out.println("Sentence Case String: " + myString.sentenceCase());
                    break;
                case 4:
                    try {
                        System.out.print("Enter number of characters to extract: ");
                        int n = scanner.nextInt();
                        System.out.println("Extracted String: " + myString.extractRight(n));
                    } catch (Exception e) {
                        System.out.println("Error: " + e.getMessage());
                    }
                    break;
                case 5:
                    System.out.println("Exiting program.");
                    scanner.close();
                    return;
                default:
                    System.out.println("Invalid choice, please try again.");
            }
        }
    }
}

TRY IT YOURSELF

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.