Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problems adopted from CodingBat How can I implenent these programs with C++? Ple

ID: 3767829 • Letter: P

Question

Problems adopted from CodingBat

How can I implenent these programs with C++? Plese Help me !

1. Given a string, compute recursively a new string where all the 'x' chars have been removed.

noX("xaxb") "ab"

noX("abc") "abc"

noX("xx") ""

2. Given a string, compute recursively the number of times lowercase "hi" appears in the string, however do not count "hi" that have an 'x' immediately before them.

countHi2("ahixhi") 1

countHi2("ahibhi") 2

countHi2("xhixhi") 0

3. Given a string, compute recursively a new string where all the adjacent chars are now separated by a "*".

allStar("hello") "h*e*l*l*o"

allStar("abc") "a*b*c"

allStar("ab") "a*b"

4. Count recursively the total number of "abc" and "aba" substrings that appear in the given string.

countAbc("abc") 1

countAbc("abcxxabc") 2

countAbc("abaxxaba") 2

5. Given a string and a non-empty substring sub, compute recursively the number of times that sub appears in the string, without the sub strings overlapping.

strCount("catcowcat", "cat") 2

strCount("catcowcat", "cow") 1

strCount("catcowcat", "dog") 0

Explanation / Answer

1
public String noX(String str)
{
if (str.equals("")) return str;
if (str.charAt(0) == 'x') return noX(str.substring(1));
else return str.charAt(0) + noX(str.substring(1));
}

or

public String noX(String str)
{
char ch;
if(str.length() == 0)
return str;
ch = str.charAt(0);
if(ch == 'x')
return noX(str.substring(1));
return ch + noX(str.substring(1));
}

2


public int countHi(String str)
{
if(str.length() < 2)
return 0;
if(str.charAt(0) == 'h' && str.charAt(1) == 'i')
return 1 + countHi(str.substring(2));
return countHi(str.substring(1));
}


4

public int countAbc(String str)
{
String left;
if(str.length() < 3)
return 0;
left = str.substring(0, 3);
if(left.equals("abc"))
return 1 + countAbc(str.substring(3));
if(left.equals("aba"))
return 1 + countAbc(str.substring(2));
return countAbc(str.substring(1));
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote