I am writing code to create a cipher text using first a Transposition, then a substitution. So far, what I have done run fine (no substitution yet), except that the text obtained from the transposition contains non-breaking spaces.
After searching online, one of the solutions is to use text.replace("\ ", "")
, which I use in my code, but it does not help me remove those non-breaking spaces. Your help and suggestions will be very appreciated. I am using Java with Netbeans IDE 7.4.
public class Cipher {
static java.util.HashMap<String, String> HMOperators = new java.util.HashMap<String, String>();
public static char[] Alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
/**
* @param args the command line arguments
*/
static int locateindex = 0;
static int[] WhiteSpaceSeparator = new int[100];
public static String TCipher(String text)
{
char[] stringtoCharArray = text.toCharArray();
char ch;
int loc = 0;
for (int i = 0; i < text.length(); i++)
{
ch = stringtoCharArray[i];
if (Character.isSpaceChar(ch))
{
WhiteSpaceSeparator[loc] = i;
}
loc++;
}
text = text.trim();
String PlainTextNoSpace = text.replaceAll(" ", "");
char[] charArray = PlainTextNoSpace.toCharArray();
int NewIndexArray[] = {2, 7, 5, 3, 0, 4, 6, 1};
int Rows = 6;
int Cols = 8;
char TArray[][] = new char[Rows][Cols];
int i = 0;
for (int row = 0; row <= 5; row++)
{
if (i == charArray.length)
{
break;
}
TArray[row][NewIndexArray[0]] = charArray[i]; //@ column number index == 2
if ((i + 1) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[1]] = charArray[i + 1]; //@ column number index == 7
if ((i + 2) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[2]] = charArray[i + 2]; //5
if ((i + 3) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[3]] = charArray[i + 3]; //3
if ((i + 4) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[4]] = charArray[i + 4]; //0
if ((i + 5) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[5]] = charArray[i + 5]; //4
if ((i + 6) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[6]] = charArray[i + 6]; //6
if ((i + 7) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[7]] = charArray[i + 7]; //1
i = i + 8;
}
//printing the new 2d array with columns organized in increasing order
//the new plain text has to be read from the firt column (column 0) to the last one (column 7)
for (int rows = 0; rows < 6; rows++)
{
System.out.println();
for (int col = 0; col < 8; col++)
{
System.out.print(TArray[rows][col]);
}
}
//Reading the new plain test per column
char[] NewTChar = new char[Rows * Cols];
int k = 0;
for (int col = 0; col < 8; col++)
{
System.out.println();
for (int rows = 0; rows < 6; rows++)
{
ch = TArray[rows][col];
NewTChar[k] = ch; //storing all the columns into a 1d array to obtain a new text (new cipher)
k++;
}
}
int n = 0;
String SeconPlainText = new String(NewTChar);
SeconPlainText = SeconPlainText.trim();
SeconPlainText = SeconPlainText.replace("\u00A0", ""); //It does not work, I am not sure why
System.out.println(SeconPlainText);
System.out.println(SeconPlainText);
StringTokenizer st = new StringTokenizer(SeconPlainText);//it seems like the string is only one token
while (st.hasMoreTokens())
{
System.out.println(st.nextToken()); //it prints the whole string, instead of each token
}
int a = st.countTokens();
return SeconPlainText;
}
public static void main(String[] args)
{
String PlainText = "Machines take me by surprise with great frequency";
String Ttext = TCipher(PlainText);
// SCipher(Ttext);
}
}
I actually edited one of my for loops such that I have to force to store an actual white space character when a non-breaking space is encountered. Thus, when the char array is converted into a string, I can later remove the white space from the string using the text.replace(" ",""); Now, it works as expected. However, in the IDE (Netbeans), "isJavaLetter" is crossed out. I am not sure if it means it is deprecated, but it helped me do what I want. Below is my updated for loop:
for (int col = 0; col < 8; col++)
{
System.out.println();
for (int rows = 0; rows < 6; rows++)
{
ch = TArray[rows][col];
if(Character.isJavaLetter(ch))//
NewTChar[k] = ch; //storing all the columns into a 1d array to obtain a new text (new cipher)
else
NewTChar[k] =' '; //storing an actual white space character
k++;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.