everyNth

Given a non-empty string and an int N, return the string made starting with char 0, and then every Nth char of the string. So if N is 3, use char 0, 3, 6, … and so on. N is 1 or more.

everyNth(”Miracle”, 2) ? “Mrce”
everyNth(”abcdefg”, 2) ? “aceg”
everyNth(”abcdefg”, 3) ? “adg”

public String everyNth(String str, int n) {
//char c[]=str.toCharArray();
String str1="";
for(int i=0;i<str.length();i++)
{ if(i%n ==0)
str1=str1+str.substring(i,i+1);
}
return str1;
}

, , ,

No Comments

endUp

Given a string, return a new string where the last 3 chars are now in upper case. If the string has less than 3 chars, uppercase whatever is there. Note that str.toUpperCase() returns the uppercase version of a string.

endUp(”Hello”) ? “HeLLO”
endUp(”hi there”) ? “hi thERE”
endUp(”hi”) ? “HI”

public String endUp(String str) {
if(str.length() < 3) return(str.toUpperCase());
else return str.substring(0,str.length()-3)+str.substring(str.length()-3).toUpperCase();

}

, , , ,

No Comments

lastDigit

Given two non-negative int values, return true if they have the same last digit, such as with 27 and 57. Note that the % “mod” operator computes remainders, so 17 % 10 is 7.

lastDigit(7, 17) ? true
lastDigit(6, 17) ? false
lastDigit(3, 113) ? true

public boolean lastDigit(int a, int b) {
if(a%10 == b%10) return true;
else return false;
}

, ,

No Comments

stringE

Return true if the given string contains between 1 and 3 ‘e’ chars.

stringE(”Hello”) ? true
stringE(”Heelle”) ? true
stringE(”Heelele”) ? false

public boolean stringE(String str) {
int n=0;
char str1[]=str.toCharArray();
for(int i=0;i<str1.length;i++)
if(str1[i]=='e') n++;

if(n==1 || n==3) return true;
else return false;
}

, ,

No Comments

max1020

Given 2 positive int values, return the larger value that is in the range 10..20 inclusive, or return 0 if neither is in that range.

max1020(11, 19) ? 19
max1020(19, 11) ? 19
max1020(11, 9) ? 11

public int max1020(int a, int b) {
int temp;
if(a>b) temp=a;
else temp=b;

if (temp >= 10 && temp <= 20) return temp;
if (b >= 10 && b <= 20) return b;
if (a >= 10 && a <= 20) return a;

return 0;
}

,

No Comments

in3050

Given 2 int values, return true if they are both in the range 30..40 inclusive, or they are both in the range 40..50 inclusive.

in3050(30, 31) ? true
in3050(30, 41) ? false
in3050(40, 50) ? true

public boolean in3050(int a, int b) {
if((a>=30 && a<=40)&&(b>=30 && b<=40)) return true;
else if((a>=40 && a<=50)&&(b>=40 && b<=50)) return true;
else return false;
}

,

No Comments

intMax

Given three int values, A B C, return the largest.

intMax(1, 2, 3) ? 3
intMax(1, 3, 2) ? 3
intMax(3, 2, 1) ? 3

public int intMax(int a, int b, int c) {
if(a>b)
if(a>c) return a;
else return c;
else if(b>c) return b;
else return c;
}

, ,

No Comments