Write a method header on line two with the following specs:
Returns: a String Name: surround Parameters: a String called s a char 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:
- surround("abcabcabc",'c') ==> "ab(c)ab(c)ab(c)"
- surround("technology",'o') ==> "techn(o)l(o)gy"
Solution
1
2
3
4
5
6
7
8
9
10
11
12
|
class Main {
public static String surround(String s, char search_term)
{
return s.replaceAll(String.valueOf(search_term),"("+search_term+")");
}
//test case below (dont change):
public static void main(String[] args){
System.out.println(surround("abcabcabc",'c')); //"ab(c)ab(c)ab(c)"
System.out.println(surround("technology",'o')); //"techn(o)l(o)gy"
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
058 - Scope and Global Variables 1 (0) | 2019.12.26 |
---|---|
057 - Accumulator Method String Challenge 1 (optional) (0) | 2019.12.26 |
055 - Accumulator Method String Practice 3 (0) | 2019.12.26 |
054 - Accumulator Method String Practice 2 (0) | 2019.12.26 |
053 - Accumulator Method String Practice 1 (0) | 2019.12.26 |