Write a method header on line two with the following specs:
Returns: a String Name: censorLetter Parameters: a String called s a char called ch |
Then complete the method by programming the following behavior
Replace all instances of ch with a "*" within the String s. See below examples.
Examples:
- censorLetter("computer science",'e') ==> "comput*r sci*nc*"
- censorLetter("trick or treat",'t') ==> "*rick or *rea*"
Solution
1
2
3
4
5
6
7
8
9
10
11
12
|
class Main {
public static String censorLetter(String s,char ch)
{
return s.replaceAll(String.valueOf(ch), "*");
}
//test case below (dont change):
public static void main(String[] args){
System.out.println(censorLetter("computer science",'e')); //"comput*r sci*nc*"
System.out.println(censorLetter("trick or treat",'t')); //"*rick or *rea*"
}
}
|
반응형
'Java_beginner(Repl.it) > Auto-Graded-Course(AP CS A)' 카테고리의 다른 글
057 - Accumulator Method String Challenge 1 (optional) (0) | 2019.12.26 |
---|---|
056 - Accumulator Method String Practice 4 (0) | 2019.12.26 |
054 - Accumulator Method String Practice 2 (0) | 2019.12.26 |
053 - Accumulator Method String Practice 1 (0) | 2019.12.26 |
052 - Accumulator Method Challenge 2 (optional) (0) | 2019.12.26 |