number of occurrences of a character in a string in python code example
Example 1: count characters in string python
>>> sentence = 'Mary had a little lamb'
>>> sentence.count('a')
4
Example 2: python count character occurrences
str1.count("a")
Example 3: number of occurrences of character in string
public class CountStringOccurence {
public static void main(String[] args) {
int count= countOccurences("aaassssddadad",'s');
System.out.println(count);
}
private static int countOccurences(String word, char character){
int count = 0;
for(int i = 0; i < word.length(); i++){
if(word.chartAt(i) == character){
count++; }
}
return count;
}
}