Knowee
Questions
Features
Study Tools

Develop a program with a method to print the count of a given character in a string and return the sum of the respective indices.Hint: use charAt method to fetch a character in a string. String s="apple"; s.charAt(0) fetches the first character.

Question

Develop a program with a method to print the count of a given character in a string and return the sum of the respective indices.Hint: use charAt method to fetch a character in a string. String s="apple"; s.charAt(0) fetches the first character.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Java program that does what you asked for:

public class Main {
    public static void main(String[] args) {
        String s = "apple";
        char c = 'p';
        System.out.println("Count of '" + c + "' in " + s + ": " + charCount(s, c));
        System.out.println("Sum of indices of '" + c + "' in " + s + ": " + sumOfIndices(s, c));
    }

    public static int charCount(String s, char c) {
        int count = 0;
        for(int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == c) {
                count++;
            }
        }
        return count;
    }

    public static int sumOfIndices(String s, char c) {
        int sum = 0;
        for(int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == c) {
                sum += i;
            }
        }
        return sum;
    }
}

This program defines two methods: charCount and sumOfIndices. charCount iterates over the string and increments a counter each time it encounters the specified character. sumOfIndices does something similar, but instead of counting occurrences, it adds up the indices at which the character occurs. The main method tests these two methods with the string "apple" and the character 'p'.

This problem has been solved

Similar Questions

Tom is playing scrabble and forms new words. He wants to count the number of characters present in the word formed by him. Help him to do this using the below java program.Code :import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); //find length and print the same          int len=  ;                System.out.println("The length of the word is "+)  ; }}

Write a program that accepts an input string from the user and prints the total number of alphabet characters (A-z) in the string to the output.

Write a Python program to count the number of occurrences of a specific character in a string

Define a function print_all_chars(string1). The function prints all the characters in a given string using while loop and return the number of charachters in the string.Exampleprint_all_chars('python') -> 6> p> y> t> h> o> nHintlen() returns the length of a string.the element on index i can be accessed by using string[i].

The function LEN() returns the total number of characters in a string

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.