Bushan SC
BAN USER//using 2 for loops
public class Pattern1 {
public static void main(String[] args) {
int size = 11; //here you can give the size whatever you want
for(int i = 1; i <= size; i=i+2) {
for(int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
/*
This will also check the range of the IP address i.e <= 255
*/
public class ValidateIPAddress {
public static void main(String[] args) {
String ipAddresses[] = {"192.168.1.100", "1092.1688.1.100", "12.ab.c.235", "abv.cdd.cd.a", "192.168.1.45e"};
String patternString = "\\d+.\\d+.\\d+.\\d+";
StringBuffer valid = new StringBuffer();
StringBuffer invalid = new StringBuffer();
for(int i = 0; i < ipAddresses.length; i++) {
if(isValid(ipAddresses[i], patternString)) {
valid.append(ipAddresses[i]+"\n");
}else {
invalid.append(ipAddresses[i]+"\n");
}
}
System.out.println("Valid ip address:\n"+valid.toString());
System.out.println("InValid ip address:\n"+invalid.toString());
}
private static boolean isValid(String string, String patternString) {
String array[] = string.split("\\.");
if(array.length != 4) {
return false;
}else {
for(int i = 0; i < array.length; i++) {
if(array[i].matches(".*[a-zA-Z].*") || !(Integer.parseInt(array[i]) <= 255)) {
return false;
}
}
}
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(string);
boolean b = matcher.matches();
return b;
}
}
/*
*
Valid ip address:
192.168.1.100
InValid ip address:
1092.1688.1.100
12.ab.c.235
abv.cdd.cd.a
192.168.1.45e
* */
public class MoveZerosToLast {
public static void main(String[] args) {
int[] a = { 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9 };
int j = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] != 0) {
if (i == j) {
j++;
} else {
a[j] = a[i];
a[i] = 0;
j++;
}
}
}
System.out.println(Arrays.toString(a));
}
}
public class ReverseWords {
public static void main(String[] args) {
System.out.println(reverseWords("The Sky is Blue"));
System.out.println(reverseWords("My name is Bushan"));
}
public static String reverseWords(String string){
String words[] = string.split(" ");
int last = words.length - 1;
for(int first = 0; first < last; first++, last--){
String temp = words[first];
words[first] = words[last];
words[last] = temp;
}
//In java-8
String str = String.join(" ", words);
return str;
}
}
/*
Input: The Sky is Blue
Output: Blue is Sky The
Input: My name is Bushan
Output: Bushan is name My
*/
public class ReverseWords {
public static void main(String[] args) {
System.out.println(reverseWords("The Sky is Blue"));
System.out.println(reverseWords("My name is Bushan"));
}
public static String reverseWords(String string){
String words[] = string.split(" ");
String temp = "";
for(int i = words.length - 1; i >= 0; i--){
temp += words[i]+" ";
}
return temp.trim();
}
}
public String interchange(String s) {
String vowels = "aeiouy";
// array counter for count amount of each vowels in s
int[] counter = new int[300];
for (char c : s.toCharArray()) {
if (vowels.contains(c + "")) {
counter[c]++;
}
}
// build answer
StringBuilder builder = new StringBuilder();
for (char c : s.toCharArray()) {
// if c is vowels, search remain vowels in
// counter from 'a' to 'z' and append to answer
if (vowels.contains(c + "")) {
for (char x = 'a'; x <= 'z'; x++) {
// counter[x] > 0 mean vowels x is available
if (counter[x] > 0) {
builder.append(x);
counter[x]--;
break;
}
}
} else { // append other character to answer
builder.append(c);
}
}
return builder.toString();
}
/*
This program is not working for the below test cases:
Input : snahe
Output : snahe
Expected Output : sneha
Input : bashun
Output : bashun
Expected Output : bushan
If the vowels is in order in a string, then interchange will not happen.
In our first test case, we are expecting a should interchange with e.
*/
- Bushan SC July 09, 2018