Ignoring the effect of a space when applying encryption I am writing an encryption program that will encrypt the message "STOP POLLUTION" with the encryption function f(p) = (3p + 7) mod 26 where p is given letter.

Here is my code for doing so (tested it works):

    static final int SPACE = -33;
    static int[] convert(String s) {
        char[] array = s.toCharArray();
        int[] distances = new int[s.length()];
        for(int c=0;c&lt;distances.length; c++) {
            distances[c] = array[c] - 'A';
        }
        return distances;
    }
    static String convertBack(int[] distances) {
        String result = "";
        for(int c=0;c&lt;distances.length;c++) {
            char distance = (char) ('A' + distances[c]);
            result += distance;
        }
        return result;
    }
    static void applyShift(int[] distances, int a, int b, int mod) {
        for(int c=0;c&lt;distances.length;c++) {
            if(distances[c]!=SPACE){
                distances[c] = (a*distances[c] + b) % mod;
            }
        }
    }


My question is regarding the space in "STOP POLLUTION". I don't really care about that space in converting the String into an array of distances from zero or converting that array back to a String but I do care about the space when applying the encryption algorithm. I want to leave the space as it is because if I change it, I won't be able to write out the space in the encrypted form.

I do this by checking if the current letter I am processing is a space. To me, this is inefficient because there is only one space. Is there a better way to do this (some way to ignore the checking of the space) but still have the space stay the same?
Before I answer your question, I'm gonna give a more general review:


Spaces are good things. They help readability a lot. For example, this:

for(int c=0;c&lt;distances.length;c++) {


should be this:

for (int c = 0; c &lt; distances.length; c++) {


The rules I use are complicated, but it boils down to a space on either side of a keyword, binary operator, or curly brace.
Put a newline between your methods. After the closing brace of one, you should have a blank line, then any Javadocs for the next method, then the next method itself.
convert should probably be a private static, and the rest should be public static. It's very rare that you actually want to use package-private things; more often than not, the other three permissions cover every case.
Use advanced for loops when the only thing you use the index for is accessing a single array.
Instead of repeated + on Strings, use a StringBuilder and .append. For example, this:

String result = "";
for (int c : distances) {
    char distance = (char) ('A' + c);
    result += distance;
}
return result;


becomes this:

StringBuilder result = new StringBuilder(distances.length);
for (int c : distances) {
    char distance = (char) ('A' + c);
    result.append(distance);
}
return result.toString();


It's more performant in terms of speed and memory.
Use better variable names than array, c, etc. Those tell us nothing about what they really are.


Now, on to your question: Yes, you can do this. In fact, there are a couple of ways to do it. You've gone for the most obvious -- and, for only one or two letters, arguably best -- option. However, as you start to have more and more letters you want to ignore, it becomes a hassle to add them all to the if.

One thing you could do is declare a static final char[] forbiddenLetters and, each iteration, check if the letter (or, more accurately, 'A' + convertedLetter) is contained in forbiddenLetters, skipping over it if so.

Another option is, instead of using a blacklist, use a whitelist: Have a static final char[] validLetters and use it much the same way, but applying the encryption if the letter is in the list or not. This works better for your case, as I think you'll probably have a good deal more forbidden letters than valid ones. If you just want capital letters, you could also apply the same concept by checking that for each (converted) letter \$l\in\mathbb{Z}\$, \$l\in\left[0,25\right]\$

I'm gonna leave the implementation of that up to you, though. Both ought to be fairly easy -- especially the second suggestion in the latter -- to translate to Java.