Write a class definition for a class named S that defines a single method , omit
ID: 3536506 • Letter: W
Question
Write a class definition for a class named S that defines a single method , omitSecondWord . The method receives a String consisting of words separated by single space characters with a period at the end. For example: "This might be easy."
The method returns the String with the second word omitted. The returned value will still consist of words separated by SINGLE space characters . So, if the argument were "Yankees 3, Red Sox 4." your code will return Yankees Red Sox 4. .
If no second word exists, the method returns its argument unchanged.
Explanation / Answer
public class S{
public String omitSecondWord(String str){
String[] A = str.split("\s");
if (A.length>=2){
String ans = A[0];
int i=2;
while (i<A.length){
ans = ans + A[i++];
}
return ans;
}
else return str;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.