-1

Hello I try to print in an array of Strings In the following way:

  • Input: big = "12xy34", small = "xy" output: "** xy **"
  • Input: big = "" 12xt34 "", small = "xy" output: "******"
  • Input: big = "12xy34", small = "1" output: "1 *****"
  • Input: big = "12xy34xyabcxy", small = "xy" output: "** xy ** xy *** xy"
  • Input: big = "78abcd78cd", small = "78" output: "78 **** 78 **"

What I need to write a condition to receive as up?

 public static String stars(String big, String small) {
      //throw new RuntimeException("not implemented yet ");
     char[] arr = big.toCharArray();
    for (int i = 0; i < arr.length; i++) {
        if (big.contains(small) ) {
            arr[i] = '*';
        }
    }
        String a = Arrays.toString(arr);
     return big+""+a;

}

JavaHopper
  • 5,567
  • 1
  • 19
  • 27
itay izraelov
  • 47
  • 1
  • 1
  • 5
  • your java program is incomplete, horribly formatted and doesn't really explain how things should be going together. It's unclear how to help you. – Marcus Müller Aug 06 '16 at 21:24

1 Answers1

1

Algorithm:

  1. Convert big and small String's to char[] array's bigC and smallC respectively
  2. Iterate over each character of big String
  3. At every index during iteration, identify whether there is a sub-string possible beginning current character
  4. If there is a sub-string possibility, advance the index in big String iteration by length of small String
  5. Otherwise, replace the character by *

Code:

public class StringRetainer {

    public static void main(String args[]) {
        String big[] = {"12xy34", "12xt34", "12xy34", "12xy34xyabcxy", "78abcd78cd"};
        String small[] = {"xy", "xy", "1", "xy", "78"};
        for(int i = 0; i < big.length & i < small.length; i++) {
            System.out.println("Input: big = \"" + big[i] + "\", small = \"" + small[i] + "\" output : \"" + stars(big[i], small[i]) + "\"");
        }
    }

    public static String stars(String big, String small) {
        //String to char[] array conversions
        char[] bigC = big.toCharArray();
        char[] smallC = small.toCharArray();
        //iterate through every character of big String and selectively replace
        for(int i = 0; i < bigC.length; i++) {
            //flag to determine whether small String occurs in big String
            boolean possibleSubString = true;
            int j = 0;
            //iterate through every character of small String to determine
            //the possibility of character replacement
            for(; j < smallC.length && (i+j) < bigC.length; j++) {
                //if there is a mismatch of at least one character in big String
                if(bigC[i+j] != smallC[j]) {
                    //set the flag indicating sub string is not possible and break
                    possibleSubString = false;
                    break;
                }
            }
            //if small String is part of big String,
            //advance the loop index with length of small String
            //replace with '*' otherwise
            if(possibleSubString)
                i = i+j-1;
            else
                bigC[i] = '*';
        }
        big = String.copyValueOf(bigC);
        return big;
    }

}

Note:

  1. This is one possible solution (legacy way of doing)
  2. Looks like there is no straight forward way of making this happen using built-in String/StringBuffer/StringBuilder methods
JavaHopper
  • 5,567
  • 1
  • 19
  • 27