Print Phrases with Prime size from a Sentence


View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Given a string S, the duty is to print all phrases with prime size within the given string.


Enter: S = “It is a python programming language”
Output: is
Rationalization: Size of is is 2 and size of programming is 11 each are primes

Enter: S = “You’re utilizing geeksforgeeks”
Output: You

Strategy: To resolve the issue comply with the under steps:

  • First cut up the given string to get an array of house separated strings.
  • Begin traversing the phrases from left to proper.
    • Calculate the size of every phrase.
    • If the size is prime, then print the phrase.

Under is the implementation of the above method:



from math import sqrt


def isPrime(x):

    prime = 0

    if(x > 1):

        for i in vary(2, int(sqrt(x)) + 1):

            if (x % i == 0):

                prime = 1


        if (prime == 0):

            return True


            return False


        return False


def printPrimeLenWords(s):



    s = s.cut up()

    for i in s:




        if isPrime(len(i)):



if __name__ == '__main__':

    st = "It is a python programming language"




Time complexity: O(n * sqrt(x)), the place n is the variety of phrases within the given sentence and x be the size of the most important string.
Auxiliary Area: O(1)


Leave a Reply