1)Given a string and a non-negative int n, return a larger string that is n copies of the original string.
public String stringTimes(String str, int n) {
String f = "";
while(n > 0){
f += str;
n--;
}
return f;
}
2)Given a string and a non-negative int n, we'll say that the front of the
string is the first 3 chars, or whatever is there if the string is less
than length 3. Return n copies of the front;
public String frontTimes(String str, int n) {
String f = "";
if (str.length() > 3){
while(n > 0){
f += str.substring(0,3);
n--;
}
}else {
while(n > 0){
f += str;
n--;
}
}
return f;
}
3)Count the number of "xx" in the given string. We'll say that overlapping is allowed, so "xxx" contains 2 "xx"
int countXX(String str) {
int xx = 0;
int y = 0;
while(y <= str.length()-2){
if("xx".equals(str.substring(y,y+2))){
xx += 1;
}
y++;
}
return xx;
}
No comments:
Post a Comment