Write a method header on line two with the following specs:
Returns: a String Name: surroundStr Parameters: a String called s a String called search_term |
Then complete the method by programming the following behavior
Return a new String built from s that has every instance of the search term surrounded by parentheses See below examples.
Examples:
- surroundStr("abcabcabc","abc") ==> "(abc)(abc)(abc)"
- surroundStr("there, on planeth hoth","th") ==> "(th)ere, on plane(th) ho(th)"
Please note that you are not allowed to use the String method .replace() or .replaceAll() in your solution.
Solution
1
2
3
4
5
6
7
8
9
10
11
12
|
class Main {
public static String surroundStr(String s, String search_term)
{
return s.replaceAll(search_term, "("+search_term+")");
}
//test case below (dont change):
public static void main(String[] args){
System.out.println(surroundStr("abcabcabc","abc")); //"(abc)(abc)(abc)"
System.out.println(surroundStr("there, on planeth hoth","th")); //"(th)ere, on plane(th) ho(th)"
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
059 - Scope and Global Variables 2 (0) | 2020.02.03 |
---|---|
058 - Scope and Global Variables 1 (0) | 2019.12.26 |
056 - Accumulator Method String Practice 4 (0) | 2019.12.26 |
055 - Accumulator Method String Practice 3 (0) | 2019.12.26 |
054 - Accumulator Method String Practice 2 (0) | 2019.12.26 |