Neil Bahog Lagay

 avatar
unknown
java
3 years ago
2.8 kB
6
Indexable
import java.util.Scanner;

public class January9 {

    static String codeString = "";
    static String reversedString = "";


    static int index=0;

    public static void convertToReveresedString(String userInput) {
        String nstr = "";
        char ch;

        for (int i = 0; i < userInput.length(); i++) {
            ch = userInput.charAt(i); // extracts each character
            nstr = ch + nstr; // adds each character in front of the existing string
        }

        reversedString = nstr;

        System.out.println("\nReversed word: " + nstr);
    }

    public static void convertToCode(String userInput) {

        codeString = "";
        int inputLength = userInput.length();

        char[] lowerCaseChar = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
                'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 32 };
        char[] upperCaseChar = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
                'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 32 };
        String[] cardinalNum = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16",
                "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "0" };

        for (int i = 0; i < inputLength; i++) {
            for (int j = 0; j < 27; j++) {
                if (userInput.charAt(i) == lowerCaseChar[j] || userInput.charAt(i) == upperCaseChar[j]) {
                    codeString += cardinalNum[j]+"-";
                }
            }
        }

        String codeStringFinal = codeString.substring(0, codeString.length()-1); //para pangkuha ni sa last dash. source: https://www.journaldev.com/18361/java-remove-character-string#:~:text=Java%20String%20Remove%20Character%20and%20String%20Example

        System.out.println("\nCode: "+ codeStringFinal);

    }
                                                                                                    // MAIN METHOD
    public static void main(String[] args) {

        Scanner cin = new Scanner(System.in);

        String userInput[] = new String[5];

        int retry;


        do {

            cin.nextLine();

            String ordinalNum[] = { "First", "Second", "Third", "Fourth", "Fifth" };
            System.out.print("Input the " + ordinalNum[index] + " string: ");

            userInput[index] = cin.nextLine();

            convertToReveresedString(userInput[index]);
            convertToCode(reversedString);

            System.out.println("Do you wish to continue? 1-Yes, 0-No");
            retry = cin.nextInt();

            index++;

        } while (retry == 0);


        System.out.println("Program Terminated");

    }

}
Editor is loading...