Friday, October 14, 2022
HomeWordPress DevelopmentPrint Phrases with Prime size from a Sentence

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.

Examples:

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

Enter: S = “You might be utilizing geeksforgeeks”
Output: You
are
utilizing
geeksforgeeks

Strategy: To unravel the issue observe the under steps:

  • First break 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.

Beneath is the implementation of the above strategy:

Python3

  

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

                break

        if (prime == 0):

            return True

        else:

            return False

    else:

        return False

          

def printPrimeLenWords(s):

    

    

    s = s.break up()

    for i in s:

        

        

        

        if isPrime(len(i)):

            print(i)

  

if __name__ == '__main__':

    st = "This can be a python programming language"

      

    

    printPrimeLenWords(st)

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 biggest string.
Auxiliary House: O(1)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments