Question

Given a String str, and a char c return the number of times that c appears...

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;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Count {

    public int countProblem(String str, char c) {
        int res = 0;
        for (int i = 0; i < str.length(); i++) {
            if (Character.toLowerCase(str.charAt(i)) == c) {
                res++;
            }
        }
        return res;
    }
}
Add a comment
Know the answer?
Add Answer to:
Given a String str, and a char c return the number of times that c appears...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and...

    ​​​​​​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...

  • c++ int count(const string& str, char a); Write a recursive function that finds the number of...

    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.

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #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);...

    #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 <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str);...

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str); int numUppltr(char *str); int numLwrltr(char *str); int punChar(char *str); char*readString(char *str); int main() { char givString[MAX_LEN]; puts("Enter your string(Max 255 characters):"); //readString(givString); gets(givString); printf("\nnumber of words :%d",numWords(givString)); printf("\nnumber of uppercase letters %d",numUppltr(givString)); printf("\nnumber of lowercase letters %d",numLwrltr(givString)); printf("\nnumber of punctuations %d\n",punChar(givString)); printf("\nnumber of digits:%d\n",numDigit(givString)); system("pause"); return 0; } char *readString(char *str) { int ch, i=0; while((ch=getchar())!=EOF && ch!='\n') { if(i) { str[i]=ch; i++; }...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • public static String getFlag(int size, char color1, char color2, char color3) - This method returns a...

    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.

  • C++ LANGUAGE /** * Given an input string, count the number of words ending * in...

    C++ LANGUAGE /** * Given an input string, count the number of words ending * in 'y' or 'z' -so the 'y' in "heavy" and the 'z' in "fez" * count, but not the 'y' in "yellow". Make sure that your * comparison is not case sensitive. We'll say that a y * or z is at the end of a word if there is not an alphabetic * letter immediately following it. * * Do not use any string...

  • Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray();...

    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...

  • Java Assignment Calculator with methods. My code appears below what I need to correct. What I...

    Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this:     public static double divide(double operand1, double operand2) {         return operand1 / operand2;     } The random method is supposed to return a double within a lower limit and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT