THIS IS A Beginner Course_Introduction to JAVA Note: You Must use API only(Metho
ID: 3863994 • Letter: T
Question
THIS IS A Beginner Course_Introduction to JAVA
Note: You Must use API only(Method on String)
Given a string, print true if the number of appearances of "is" anywhere in the string is equal to the number of appearances of "not" anywhere in the string (case sensitive).
Sample 1
equalIsNot("This is not") false
Sample 2
equalIsNot("This is notnot") true
Sample 3
equalIsNot("noisxxnotyynotxisi") true
Also, Given a string, replace all the white spaces with “V_V” and print out the new string.
Sample 1
replaceWhiteSpace(“How are you?) “HowV_VareV_Vyou?”
Sample 2
replaceWhiteSpace(“I am good?) “IV_VamV_Vgood?"
PLEASE Do NOT use advanced high level syntax.
PLEASE Do NOT use advanced libraries beyond a beginner level.
Explanation / Answer
code:
public static int count(String str, String substr)
{
int cnt = 0;
int id = 0;
while ((id = string.indexOf(substr, id)) != -1)
{
id++;
cnt++;
}
return cnt;
}
public static bool equalIsNot(String str)
{
if(count(str,"is")==count(str,"not")
return true;
else
return false;
}
public static String replaceWhiteSpace(String str)
{
return str.replaceAll(" ",“V_V”);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.