Have to make a String method called cryptocode-can't figure it out

Here's the question: cryptocode accepts a string message. It will scramble the alphabet, then use it to encode the string object so that the first letter in the scrambled alphabet will replace the letter 'A', the second letter will replace the 'B's and so on (encoded string is returned).

HOW’S IT GOING?  with scrambled alphabet HOAZXJRTUYBIVEWKLSNCDMFGPQ   becomes
TWF'N UC RWUER?

I don't get how to replace the letters after generating the "new" alphabet

here's what i have done:

public static String cryptocode (String cryptic)
{
    sc = new Scanner (System.in); 
    clear();

    StringBuffer alphabet = new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    String holder = "";

    //Generates a "new" alphabet
    for (int x=1; x<=100; x++)
    {   
        char temp;
        int num1 = (int)(Math.random()*alphabet.length());
        int num2 = (int)(Math.random()*alphabet.length());

        temp = alphabet.charAt(num1);
        alphabet.setCharAt(num1, alphabet.charAt(num2));
        alphabet.setCharAt(num2, temp);
    }

    cryptic = cryptic.toUpperCase();
    System.out.println(alphabet);

    for (int x= 0; x < cryptic.length(); x++)
    {
        //Can't figure out how to replace the letters using the new alphabet
    }

    return holder;
}

Answers

  • Answer ✓
    void setup() {
      cryptocode("HEllo");
    }
    
    public static String cryptocode (String cryptic)
    {
      //sc = new Scanner (System.in); 
      //clear();
    
      StringBuffer alphabet = new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
      String holder = "";
    
      //Generates a "new" alphabet
      for (int x=1; x<=100; x++)
      {   
        char temp;
        int num1 = (int)(Math.random()*alphabet.length());
        int num2 = (int)(Math.random()*alphabet.length());
    
        temp = alphabet.charAt(num1);
        alphabet.setCharAt(num1, alphabet.charAt(num2));
        alphabet.setCharAt(num2, temp);
      }
    
      println(alphabet); 
    
      cryptic = cryptic.toUpperCase();
      System.out.println(alphabet);
    
      String newStr ="";
    
      for (int x= 0; x < cryptic.length(); x++)
      {
        //Can't figure out how to replace the letters using the new alphabet
        println(int(cryptic.charAt(x)-65));
        newStr+=alphabet.charAt(int(cryptic.charAt(x)-65));
      }
    
      println(newStr); 
    
      return newStr;
    }
    
  • Thank you very much

Sign In or Register to comment.