Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
3.0 kB
5
Indexable
Never
import java.util.Scanner;

public class Sheet1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your name");
        String name = scanner.nextLine();
        int option = 1;
        String choice = "yes";
        String str11 = " ";
        String allupper = " ";
        String allLower = " ";
        String result = " ";

        do {
            System.out.println("Enter an option (1-3)");
            System.out.println("1- counts the number of vowels");
            System.out.println("2- Checks whether the first name is a palindrome or not");
            System.out.println("3- Makes the first letter of each name capitalized");
            option = scanner.nextInt();
            scanner.nextLine();  

            if (option == 1) {
                str11 = name.toLowerCase();
                int vowlsNumber = 0;
                for (int i = 0; i < name.length(); i++) {
                    char v = str11.charAt(i);
                    if (v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u') {
                        vowlsNumber++;
                    }
                }
                System.out.println("Number of vowels: " + vowlsNumber);
            } else if (option == 2) {
                String str2 = name.split(" ")[0];
                boolean isPal = true;
                for (int i = 0, j = str2.length() - 1; i < j; i++, j--) {
                    if (str2.charAt(i) != str2.charAt(j)) {
                        isPal = false;
                        break;
                    }
                }
                if (isPal) {
                    System.out.println("First name is a palindrome");
                } else {
                    System.out.println("First name is not a palindrome");
                }
            } else if (option == 3) {
                allupper = name.toUpperCase();
                allLower = name.toLowerCase();
                result = allupper.charAt(0) + "";
                boolean isSpaceBefore = false;

                for (int i = 1; i < name.length(); i++) {
                    char ch = name.charAt(i);
                    if (isSpaceBefore) {
                        result = result + allupper.charAt(i);
                        isSpaceBefore = false;
                    } else {
                        result = result + allLower.charAt(i);
                    }
                    if (ch == ' ') {
                        isSpaceBefore = true;
                    }
                }
                System.out.println("The new name is: " + result);
            }

            System.out.println("Do you want to make another choice?");
            System.out.println("Enter yes or no");
            choice = scanner.nextLine().toLowerCase();
             if (choice.equals("no"))
 break;

        } while (true);
    }
}