Find if the characters in the given string are unique & not repeating. If the string has all the unique characters then replace the spaces with %20 & print the changed string.
Example : If the given string is abcd yu then output will be -
true
abcd%20yu
If the given string is abcda yu then the output will be -
false
import java.lang.reflect.Field;
public class Unique {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String str = "abcda yu";
Field fStr = String.class.getDeclaredField("value");
fStr.setAccessible(true);
char[] chr = (char[])fStr.get(str);
char[] chr1 = new char[chr.length+2];
int checker = 0;
int j = 0;
for (int i = 0; i < str.length(); ++i) {
int val = str.charAt(i) - 'a';
if(str.charAt(i) == ' ') {
chr1[j] = '%';
chr1[++j] = '2';
chr1[++j] = '0';
} else {
chr1[j] = chr[i];
}
j++;
if ((checker & (1 << val)) > 0) {
System.out.println(false);
System.exit(0);
}
checker |= (1 << val);
}
fStr.set(str, chr1);
System.out.println(true);
System.out.println(str);
}
}
Example : If the given string is abcd yu then output will be -
true
abcd%20yu
If the given string is abcda yu then the output will be -
false
import java.lang.reflect.Field;
public class Unique {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String str = "abcda yu";
Field fStr = String.class.getDeclaredField("value");
fStr.setAccessible(true);
char[] chr = (char[])fStr.get(str);
char[] chr1 = new char[chr.length+2];
int checker = 0;
int j = 0;
for (int i = 0; i < str.length(); ++i) {
int val = str.charAt(i) - 'a';
if(str.charAt(i) == ' ') {
chr1[j] = '%';
chr1[++j] = '2';
chr1[++j] = '0';
} else {
chr1[j] = chr[i];
}
j++;
if ((checker & (1 << val)) > 0) {
System.out.println(false);
System.exit(0);
}
checker |= (1 << val);
}
fStr.set(str, chr1);
System.out.println(true);
System.out.println(str);
}
}