Count of words ending at the given suffix in Java

Given a string str consisting of a sentence, the task is to find the count of words in the given sentence that end with the given suffix suff.

Examples:

Input: str = “GeeksForGeeks is a computer science portal for geeks”, suff = “ks”
Output: 2
“GeeksForGeeks” and “geeks” are the only words ending with “ks”.



Input: str = “This is a sample string”, suff = “is”
Output: 2

Approach:

  • Extract all the words from the given sentence using the split() method.
  • Now for every word check whether the current words ends with the given suffix using endsWith() method.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG {
  
    // Function to return the count of words
    // in the given sentence that
    // end with the given suffix
    static int endingWith(String str, String suff)
    {
  
        // To store the count
        int cnt = 0;
  
        // Extract words from the sentence
        String words[] = str.split(" ");
  
        // For every word
        for (int i = 0; i < words.length; i++) {
  
            // If it ends with the given suffix
            if (words[i].endsWith(suff))
                cnt++;
        }
  
        return cnt;
    }
  
    // Driver code
    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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Function declared to
# return the count of words
# in the given sentence that
# end with the given suffix
def endingWith( str , suff ):
      
    # Variable to store count
    c = 0
      
    # split function used to extract words
    # from sentence in form of list
    wrd = str.split(" ")
      
    # using for loop with 'in' to extract 
    # elements of list
    for l in wrd:
        if l.endswith(suff):
            c += 1
              
    # returning the count
    return c
  
# Driver Code    
str = "GeeksForGeeks is a computer science portal for geeks"
suff = "ks"
  
# printing the final cde
print(endingWith(str, suff ))
  
# This code is contributed by Animesh_Gupta

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Animesh_Gupta

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.