The problem is stated in Encrypt this!:
Your message is a string containing space separated words. You need to encrypt each word in the message using the following rules:
The first letter needs to be converted to its ASCII code.
The second letter needs to be switched with the last letter
Keepin' it simple: There are no special characters in input.
Kata.encryptThis("Hello") => "72olle"
Kata.encryptThis("good")` => "103doo"
Kata.encryptThis("hello world") => "104olle 119drlo"
class NewEnpc{
public static String encryptThis(String text) {
StringBuilder sb=new StringBuilder();
String[] arry=text.split(" ");
if(text!=""){
for(int j=0;j<arry.length;j++){
char[] sd=arry[j].toCharArray();
int number=(int)sd[0];
if(sd.length>1){
for(int k=0;k<sd.length;k++){
if(k==0){
sb.append(number);
}
else if(k==1){
int len=sd.length-1;
char c=sd[len];
sb.append(c);
}
else if(k==sd.length-1){
char c=sd[1];
sb.append(c);
}
else{
char c=sd[k];
sb.append(c);
}
}
}
else{
sb.append(number);
}
sb.append(" ");
}
}
return sb.toString().trim();
}
}
public class EncryPt {
public static void main(String[]args){
System.out.println("Hello");
System.out.println(NewEnpc.encryptThis("A
wise old owl lived in an oak"));
}
}
Can someone help me improve my coding standards. I have solved the problem.
"three spaces"
(I actually wrote 3 spaces between the words, but they are not shown as such here). Your code should not crash while encrypting this text. And what about the text"hello, world"
, is it encrypted as you expect it to be? \$\endgroup\$