| | |
Cipher ALphabet Arranging help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 2
Reputation:
Solved Threads: 0
Hey guys I hope you can help me with this problem, its like a minor problem.
----PROBLEM-----
I don't know how to generate the secret code... meaning if the input was:
{
c.println (generateCipherAlphabet ("COMPUTER SCIENCE));
}
Above only outputs "COMPUTERSINQVWXYZABDFGHJKL" which is the cipher alphabet only
I want it to output "CQHOVJMWKPXLUYTZEARBSDIFNG" which is the secret code.. how do I do that? I was thinking of some for loops if it is can ya help me construct one?
----PROBLEM-----
I don't know how to generate the secret code... meaning if the input was:
{
c.println (generateCipherAlphabet ("COMPUTER SCIENCE));
}
Above only outputs "COMPUTERSINQVWXYZABDFGHJKL" which is the cipher alphabet only
I want it to output "CQHOVJMWKPXLUYTZEARBSDIFNG" which is the secret code.. how do I do that? I was thinking of some for loops if it is can ya help me construct one?
Java Syntax (Toggle Plain Text)
public class GenerateCipher { /** To generate keyPhrase into cipher alphabet and secret code * @param keyPhrase keyPhrase to be changed * @return changed keyPhrase */ public static String generateCipherAlphabet (String keyPhrase) { //defines the variables and creates alphabet string String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String realStr = ""; String newStr = ""; //for loop combined to delete the punctuations and spaces for (int index = 0 ; index < keyPhrase.length () ; index++) { char character = keyPhrase.charAt (index); if (Character.isLetter (character)) { newStr += character; } } //for loop to remove the duplicated letters from the keyPhrase for (int position = 0 ; position < newStr.length () ; position++) { char removeLetter = newStr.charAt (position); //detects for the duplicated letters remaining in keyPhrase if (realStr.lastIndexOf (removeLetter, position - 1) == -1) { realStr += removeLetter; } } //starts at the end of the keyPhrase to add the alphabets char searchLetter = realStr.charAt (realStr.length () - 1); //for loop to add the alphabet starting from the searchLetter to the end for (int position = (alphabet.indexOf (searchLetter)) ; position < alphabet.length () ; position++) { char letterAdded = alphabet.charAt (position); //if statement when there is a duplicated letter from the cipher if (realStr.lastIndexOf (letterAdded, position - 1) == -1) { realStr += letterAdded; } } //for loop that adds the alphabet from the beginning of keyPhrase for (int position = 0 ; position < alphabet.indexOf (searchLetter) ; position++) { char letterAdded2 = alphabet.charAt (position); //if statement when there is a duplicated letter from the beginning of the cipher if (realStr.lastIndexOf (letterAdded2) == -1) { realStr += letterAdded2; } } //for loop to jump the incrament of letters from length of realStr for (int position = 0 ; position < realStr.length () ; position++) { char character2 = realStr.charAt (position); int character2Pos = realStr.indexOf (character2); //for loop to build the final cipher alphabet for (int index = character2Pos ; index >= realStr.length () ; index += realStr.length ()) { realStr += realStr.charAt (index); } } return realStr; }
![]() |
Other Threads in the Java Forum
- Previous Thread: barcode reader
- Next Thread: **Running applets
Views: 1753 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addressbook android api append apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component csv database detection draw eclipse error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia linux list loop map method methods mobile netbeans newbie number object objects oracle oriented os panel print problem program programming project projects recursion replaydirector reporting researchinmotion return robot scanner score screen se server set size sms socket sort sql stream string swing test threads time transfer tree ubuntu windows





