occurences specified character code example

Example 1: python return number of characters in string

# Example usage:
your_string = "Example usage"
len(your_string)
--> 13

Example 2: count 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;
}
}

Tags: