Problem Solving with Algorithms

반응형
I solved the "Credit Card Mask" kata! Take the code initiation @
codewars.com/r/DE7rhA
to enlist and challenge yourself.
#codewars

public class Maskify {
    public static String maskify(String str) {
      int n = str.length();
      if(n > 4) {
        char[] masked = new char[n];
        for(int i = 0; i < n-4; i++) {
          masked[i] = '#';
        }
        for(int i = n-4; i < n; i++)
          masked[i] = str.charAt(i);
        str = new String(masked);
      }
      return str;
    }
}
public class Maskify {
    public static String maskify(String str) {
        if (str.length() <= 4) return str;
        String result = "";
        for (int i = 0; i < str.length()-4; i++) {
            result += "#";
        }
        return result + str.substring(str.length()-4);
    }
}
반응형
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band