কম্পিউটার

স্ট্রিং এ একটি শব্দের ঘটনা গণনা করার জন্য একটি পাইথন প্রোগ্রাম লিখুন?


এখানে ব্যবহারকারী একটি স্ট্রিং দিয়েছেন এবং ব্যবহারকারীকে ঘটনার সংখ্যা গণনা করার জন্য শব্দও দিয়েছে। আমাদের কাজ হল ঘটনার সংখ্যা গণনা করা এবং মুদ্রণ করা।

উদাহরণ

Input: Python is an interpreted high-level programming language for general purpose programming.
Enter the word to count occurrence ::>programming
Output:: 2

অ্যালগরিদম

wordoccurences(n,p)
/* n is input string and p is the word to count occurrence */
Step 1: split the string by space
Step 2: we use one counter variable c and it’s initialized by 0 and if word is match then c is incremented by 1.
Step 3: then we searching using for loop.
Step 4: if condition is true then counter c is increased and display the final result.

উদাহরণ কোড

def wordoccurences(n, p):
   x = n.split(" ")
   c = 0
   for i in range(0, len(x)):
      # if match found increase count 
      if (p == x[i]):
         c = c + 1
   return c       
# Driver code
n=input("Enter String ::>")
p=input("Enter the word to count occurrence ::>")
print("THE NUMBER OF OCCURRENCE OF A WORD ",p,"is",wordoccurences(n, p))
# To count the number of occurrence of a word in the given string 

আউটপুট

Enter String ::>python is an interpreted high level programming language for general purpose programming
Enter the word to count occurrence ::>programming
THE NUMBER OF OCCURRENCE OF A WORD programming is 2

  1. একটি বাক্যে শব্দ গণনা করার জন্য পাইথন প্রোগ্রাম

  2. পাইথন প্রোগ্রাম একটি প্রদত্ত স্ট্রিং শব্দ গণনা?

  3. পাইথন প্রোগ্রাম টিপলে একটি উপাদানের উপস্থিতি গণনা করতে

  4. একটি সংখ্যার মোট বিট গণনা করার জন্য একটি পাইথন প্রোগ্রাম লিখুন?