Count of words ending at the given suffix in Java | Java implementation of the approach ; Function to return the count of words in the given sentence that end with the given suffix ; To store the count ; Extract words from the sentence ; For every word ; If it ends with the given suffix ; Driver code
class GFG { static int endingWith ( String str , String suff ) { int cnt = 0 ; String words [ ] = str . split ( " ▁ " ) ; for ( int i = 0 ; i < words . length ; i ++ ) { if ( words [ i ] . endsWith ( suff ) ) cnt ++ ; } return cnt ; } public static void main ( String args [ ] ) { String str = " GeeksForGeeks ▁ is ▁ a ▁ computer " + " ▁ science ▁ portal ▁ for ▁ geeks " ; String suff = " ks " ; System . out . print ( endingWith ( str , suff ) ) ; } }