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?
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;
}