public static int countCharacter(String str, char c)
{
// This recursive method takes a String and a char as parameters and
// returns the number of times the char appears in the String. You may
// use the function charAt(int i) described below to test if a single
// character of the input String matches the input char.
// For example, countCharacter(“bobbie”, ‘b’) would return back 3, while
// countCharacter(“xyzzy”, ‘y’) would return back 2.
// Must be a RECURSIVE SOLUTION
// You may need to use the following methods:
// str.substring(start,end) – returns the substring from start to end-1
// str.charAt(index) – returns the character at position index of str
// str.length() – returns the length of the string str
{
}
public class MyClass {
public static int countCharacter(String s, char c)
{
if (s.length() == 0)
return 0;
else if (s.charAt(0) == c)
return 1 +
countCharacter(s.substring(1), c);
else
return
countCharacter(s.substring(1), c);
}
public static void main(String[] args) {
System.out.println(countCharacter("xyzzy", 'y'));
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me
public static int countCharacter(String str, char c) { // This recursive method takes a String and...
c++ int count(const string& str, char a); Write a recursive function that finds the number of occurrences of a specified letter in a string. For example,count("Welcome",’e’)returns 2.
Given a String str, and a char c return the number of times that c appears inside str. countChar("Abcdefg", 'a') returns 1 countChar("xxXx", 'x') returns 4 countChar("", 'q'') returns 0 ----------------- Please use java public class Count { public int countProblem(String str, char c) { int res = 0; //Your work is here return result; } }
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
Write a static method called encodeString that takes a string as input and prints to the console a word where each letter is advanced by one value. In addition, your method should return the encoded String. You may make use of the method provided below to help you. public static char encode(char x) { int current = x; current++; return (char)current; } This method takes a character and returns an "encoded" character. In other words, encode('a') will return 'b'. Your...
public static String getFlag(int size, char color1, char color2, char color3) - This method returns a string where a triangle appears on the left size of the diagram, followed by horizontal lines. For example, calling DrawingApp.getFlag(9, 'R', '.', 'Y'); will generate the string: R............................................ RRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRRRR.................................... Write a Java program and the result has to match the pattern which showed above.
JAVA: Excerpt B.A: Complete the implementation of the following method. The purpose of this method is to return true if and only if all the elements in the array are x. In other words, all elements in the array are the value x (replace -a- with right answer). public static -a- allSame(int[] nums, int x){ for (int i = 0; i < -a-; i++){ if (nums[i] -a- x){ return -a-; } } return -a- } Excerpt B.B: What is printed...
Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray(); char[] reverse = new char[original.length]; for(int i =0; i<s.length(); i++){ reverse[i] = original[original.length-1-i]; } return new String(reverse); } /** * Takes in a string containing a first, middle and last name in that order. * For example Amith Mamidi Reddy to A. M. Reddy. * If there are not three words in the string then the method will return null * @param name in...
Consider the following methods’ headers: public static int one(int a, char b, double c, String d) public static double two(double x, double y) public static char three(int r, int s, char t, double u) Answer the following questions: Q1) What is the signature of method one? Answer: Q2) What is the return type of method two? Answer: Q3) What the formal parameters of method three? Answer: Q4) How many actual parameters are needed to call the method three? Answer: Q5)...
***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...