Question: can you please help me write this code iteratively, (preferably using stacks): the code is in java, and it works to find all # characters
can you please help me write this code iteratively, (preferably using stacks):
the code is in java, and it works to find all "#" characters within a string and replace all occurences of "#" by either "X" or "O", and then output all possible combinations.
i would like to make it so that when the user inputs a string with characters of either X,O, or #... every instance of "#" is replaced by either X or O and then all possible permutations of that new string with only X or O is output.
my recursive code:
import java.io.FileNotFoundException; import java.io.PrintStream; import java.util.Scanner;
public class V1{ public static void main(String[] args) throws FileNotFoundException{ // open scanner and ask user for input Scanner input = new Scanner(System.in); System.out.println("Please enter a string with a random number of crosses" + ", naughts, and #'s:"); // measure accurate beginning runtime long startTime = System.currentTimeMillis(); String userInput = input.next(); // call the method UnHide(userInput); // measure accurate endtime System.out.println("The run times: "); long endTime = System.currentTimeMillis(); // compare values and check if file is being updated System.out.println(startTime); System.out.println(endTime); //redirect output file PrintStream fileOut = new PrintStream("Out.txt"); System.setOut(fileOut); // print in output file System.out.println(startTime); System.out.println(endTime); long totalTime = endTime - startTime; //display total time System.out.println("Total time to run the program is: " + (totalTime/1000.0) + " seconds" ); input.close(); } public static String change(String a, String b) { String temporary = a.replaceFirst("#", b); return temporary; } // UnHide method public static void UnHide(String a){
if(a.contains("#")) { UnHide(change(a, "X")); UnHide(change(a, "O")); }else { System.out.println(a); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
