Page Contents
Interview Content
- C Programming Coding Questions
- C Pattern Programming Questions
- C Programming Interview Questions
- Java Programming Coding Questions
- Java Pattern Programming Questions
- Java Programming Interview Questions
- Python Programming Coding Questions
- Python Pattern Programming Questions
- Python Programming Interview Questions
- SQL Interview Questions
Java Program to count occurrences of a character in a string
Ans:
In this tutorial we will learn how to write program in Java to count occurrence of character in a String.
Also Read this: C program to count the occurrence of a character in String.
How this Java program will behave?
This Java program will take a string and a character as an input. And it will count the occurrence of a given character. This program will also count spaces I we will give it.
For example:
If we have given a String as an input “Quescol is educational website” and also a character ‘s’.
Then after counting character ‘a’, program will return output 2.
Method to count the occurrence of given character in a string using Java.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Please give a String : ");
String str= sc.nextLine();
Scanner sc1= new Scanner(System.in);
System.out.print("Please give a char to count occurence: ");
char ch= sc1.next().charAt(0);
System.out.print("Total Number of occurence of '"+ch+"' is: ");
System.out.println(countOccurence(str, ch));
}
public static int countOccurence(String str, char ch){
char arr[] = new char[str.length()];
int count=0;
for(int i=0; i<str.length();i++ ){
arr[i] = str.charAt(i);
if(arr[i]==ch){
count++;
}
}
return count;
}
}
Output:
Also prepare these Java Programs:
- Java Program to print Fibonacci series using recursive method
- Palindrome program in Java for number using Recursive method
- Java program to check a given is an Armstrong
- Java program to add two numbers without using addition operator
- How to remove a character from a string in java
- Write a program in Java to count occurrence of a given character in a String.
- Java program to find largest and smallest number in an array
- Java program to print first duplicate number in an array of 1-100
- Java Program to remove duplicate elements from an array
- Program to count repeated elements in an array java
Latest Uploads on Website
- AVL Tree with explanation
- Radix sort algorithm explanation with example
- Quick Sort Algorithm with explanation
- Bubble sorting algorithm with Bubble sort program in C
- Insertion sort algorithm and program in C
- Selection Sort Algorithm and Program in C
- Linear probing technique explanation with example
- Collision in Hashing and Collision resolution technique
- Hashing in data structure with its types
- Binary search tree operations with Program
- Binary search tree in data structure
- Binary search algorithm in data structure with explanation
- linear search in data structure with Algo and Program