151k views
3 votes
Write a program that prompts for a line of text and then transforms the text based on chosen actions. Actions include reversing the text and converting the text to leet-speak. You may be able to imagine extending to other actions such as a pig-latin translator, or encoding or decoding a secret message. Turn in the version specified here before extending Welcome to the Text Converter Available Actions 1337) convert to 1337-speak rev) reverse the string quit) exit the progranm Please enter a string: Hello cs 200. Action (1337, rev, quit): rev 002 Sc olleH Action (1337, rev, quit): reverse again Unrecognized action. 002 Sc olleH Action (1337, rev, quit): rev ?? 11 CS 200. Action (1337, rev, quit): quit See you next time!

1 Answer

2 votes

Answer:

public class TextConverterDemo

{

//Method definition of action1337

public static String action1337(String current)

{

//Replace each L or l with a 1 (numeral one)

current = current.replace('L', '1');

current = current.replace('l', '1');

//Replace each E or e with a 3 (numeral three)

current = current.replace('E', '3');

current = current.replace('e', '3');

//Replace each T or t with a 7 (numeral seven)

current = current.replace('T', '7');

current = current.replace('t', '7');

//Replace each O or o with a 0 (numeral zero)

current = current.replace('O', '0');

current = current.replace('o', '0');

//Replace each S or s with a $ (dollar sign)

current = current.replace('S', '$');

current = current.replace('s', '$');

return current;

}

//Method definition of actionReverse

//This method is used to reverses the order of

//characters in the current string

public static String actionReverse(String current)

{

//Create a StringBuilder's object

StringBuilder originalStr = new StringBuilder();

//Append the original string to the StribgBuilder's object

originalStr.append(current);

//Use reverse method to reverse the original string

originalStr = originalStr.reverse();

//return the string in reversed order

return originalStr.toString();

}

//Method definition of main

public static void main(String[] args)

{

//Declare variables

String input, action;

//Prompt the input message

System.out.println("Welcome to the Text Converter.");

System.out.println("Available Actions:");

System.out.println("\t1337) convert to 1337-speak");

System.out.println("\trev) reverse the string");

System.out.print("Please enter a string: ");

//Create a Scanner class's object

Scanner scn = new Scanner(System.in);

//Read input from the user

input = scn.nextLine();

do

{

/*Based on the action the user chooses, call the appropriate

* action method. If an unrecognized action is entered then

* the message "Unrecognized action." should be shown on a

* line by itself and then the user is prompted again just

* as they were when an action was performed.

* */

System.out.print("Action (1337, rev, quit): ");

action = scn.nextLine();

if (action.equals("1337"))

{

input = action1337(input);

System.out.println(input);

} else if (action.equals("rev"))

{

input = actionReverse(input);

System.out.println(input);

} else if (!action.equals("quit"))

{

System.out.println("Unrecognized action.");

}

} while (!action.equals("quit"));

System.out.println("See you next time!");

scn.close();

}

}

User Angelos Chalaris
by
5.2k points