yawa

mail@pastecode.io avatar
unknown
java
2 years ago
2.9 kB
3
Indexable
Never
import java.util.Scanner;

public class January9 {

    static String unreversedString = "";
    static String reversedString = "";
    static int index=0;

    public static void convertToCode(String userInput) {

        unreversedString = "";
                                                                                                    //Unreversed String method
        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]) {
                    unreversedString += cardinalNum[j];
                }
            }

        }

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

    }
                                                                                                    //Reversed String method
    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);
    }


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

        Scanner cin = new Scanner(System.in);

        String userInput[] = new String[5];

        int retry;


        do {
            userInput[index]="";

            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++;

            cin.nextLine();

        } while (retry == 0);


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

    }

}