C++ Program RECURSION // This overloaded function finds a char in a C-string. //
ID: 3769038 • Letter: C
Question
C++ Program
RECURSION
// This overloaded function finds a char in a C-string. // It returns the position of the first occurrence of the char // specified in the first arg. // For example, find( 'p', "Pumpkin pie and whipped cream") would return 3, // because the first (lower case) p in the string is at position 3. // If sought is not in the string, the function should return UINT_MAX. // You may assume without checking that the first arg will not be the null char, unsigned find( char sought, const char * s); // Same args as the last function. // This function's job is to return the number of times the char sought // appears in the string. // For example, howMany( 'p', "Pumpkin pie and whipped cream") // would return 4 unsigned howMany( char sought, const char * s);Explanation / Answer
Answr 1.public static int find(char sought, String s)
{
return find(sought,s,0);
}
public static int find(char sought, String s, int i)
{
if(i==s.length())
return -1;
if(s.charAt(i)==sought)
return i;
return find(sought,s,i+1)
}Answer 2.
public static int howMany(char sought, String s)
{
return howMany(sought,s,0);
}
public static int howMany(char sought, String s, int i)
{ if(i==s.length())
return 0;
if(s.charAt(i)==sought)
return 1 + howMany(sought,s,i+1);
return howMany(sought,s,i+1);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.