落書きノート

ふと自分が気になった事を書いてます

Javaで問題を解く CodingBat

// frontBack

public String frontBack(String str) {
  String answer = "";
  if(!str.equals("") && str.length() > 1) {
    answer += str.charAt(str.length() - 1);
    for(int i = 1; i < str.length() - 1; i++) {
      answer += str.charAt(i);
    }
    answer += str.charAt(0);
    return answer;
  }
  else
    return str;
}

// front3

public String front3(String str) {
  String answer = "";
  for(int i = 0; i < 3; i++) {
    if(str.length() >= 3)
      answer += str.substring(0, 3);
    else if(str.length() < 3 && str.length() > 0)
      answer += str;
    else
      break;
  }
  return answer;
}

// backAround

public String backAround(String str) {
  String answer = "";
  char last = str.charAt(str.length() - 1);
  answer += last;
  for(int i = 0; i < str.length(); i++)
    answer += str.charAt(i);
  answer += last;
  return answer;
}