Java 7, 318 310 bayt
String c(String s){String x=s.toLowerCase();int i=x.indexOf("force")+1,j=x.indexOf("first")+1,t=i>0&j>i?0:j>0?1:0;return i>0|j>0?s.substring(0,t>0?(i=j):i)+(char)(s.charAt(i++)-(t>0?-6:6))+s.charAt(i++)+(char)(s.charAt(i++)+(t>0?-16:16))+(char)(s.charAt(i++)+(t>0?-15:15))+c(s.length()>i?s.substring(i):""):s;}
Tamam, bu Java için oldukça zordu ..
Açıklama:
String c(String s){ // Method with String parameter and String return-type
String x=s.toLowerCase(); // Temp String as lowercase of the input
int i=x.indexOf("force")+1, // Index of "force" + 1 (becomes 0 if NOT present; >=1 if it is present)
j=x.indexOf("first")+1, // Index of "first" + 1 (becomes 0 if NOT present; >=1 if it is present)
t=i>0&j>i?0:j>0?1:0; // Temp integer: 0 if "force" is found first; 1 if "first" is found first
return i>0|j>0? // If either "force" or "first" is found:
s.substring(0,t>0?(i=j):i) // Return the substring before that (if any) + ('f' or 'F')
+(char)(s.charAt(i++)-(t>0?-6:6)) // + 'i' <-> 'o', or 'I' <-> 'O'
+s.charAt(i++) // + 'r' or 'R'
+(char)(s.charAt(i++)+(t>0?-16:16)) // + 's' <-> 'c', or 'S' <-> 'C'
+(char)(s.charAt(i++)+(t>0?-15:15)) // + 't' <-> 'e', or 'T' <-> 'E'
+c(s.length()>i?s.substring(i):"") // + a recursive call for the rest of the input-String (if any)
: // Else:
s; // Return the input-String
} // End of method
Test kodu:
Burada deneyin.
class M{
static String c(String s){String x=s.toLowerCase();int i=x.indexOf("force")+1,j=x.indexOf("first")+1,t=i>0&j>i?0:j>0?1:0;return i>0|j>0?s.substring(0,t>0?(i=j):i)+(char)(s.charAt(i++)-(t>0?-6:6))+s.charAt(i++)+(char)(s.charAt(i++)+(t>0?-16:16))+(char)(s.charAt(i++)+(t>0?-15:15))+c(s.length()>i?s.substring(i):""):s;}
public static void main(String[] a){
System.out.println(c("Force"));
System.out.println(c("First"));
System.out.println(c("foRce"));
System.out.println(c("fiRst"));
System.out.println(c("fOrcE"));
System.out.println(c("fIrsT"));
System.out.println(c("\u0000\u0001\u0002\u0003the Force of the firsT"));
System.out.println(c("May the first be with you"));
System.out.println(c(c("May the first be with you"))); // 2x
System.out.println(c("The fIrSt of the First of the fORCE of the FIRST of the FoRCe"));
}
}
Çıktı:
First
Force
fiRst
foRce
fIrsT
fOrcE
���the First of the forcE
May the force be with you
May the first be with you
The fOrCe of the Force of the fIRST of the FORCE of the FiRSt