Count of words ending at the given suffix in Java | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; template < size_t N > void splitString ( string ( & arr ) [ N ] , string str ) { int n = 0 ; istringstream iss ( str ) ; for ( auto it = istream_iterator < string > ( iss ) ; it != istream_iterator < string > ( ) && n < N ; ++ it , ++ n ) arr [ n ] = * it ; } inline bool ends_with ( std :: string const & value , std :: string const & ending ) { if ( ending . size ( ) > value . size ( ) ) return false ; return std :: equal ( ending . rbegin ( ) , ending . rend ( ) , value . rbegin ( ) ) ; } int endingWith ( string str , string suff ) { int cnt = 0 ; const int size = 50 ; string words [ size ] ; splitString ( words , str ) ; for ( int i = 0 ; i < size ; i ++ ) { if ( ends_with ( words [ i ] , suff ) ) cnt ++ ; } return cnt ; } int main ( ) { string str = " GeeksForGeeks ▁ is ▁ a ▁ computer ▁ " " science ▁ portal ▁ for ▁ geeks " ; string suff = " ks " ; cout << endingWith ( str , suff ) ; }