Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> Python

स्ट्रिंग में किसी शब्द की घटनाओं को गिनने के लिए एक पायथन प्रोग्राम लिखें?

यहां उपयोगकर्ता ने एक स्ट्रिंग दी और उपयोगकर्ता ने घटनाओं की संख्या गिनने के लिए शब्द भी दिया। हमारा काम घटनाओं की संख्या गिनना और उसे प्रिंट करना है।

उदाहरण

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. किसी दिए गए स्ट्रिंग में शब्दों की गणना करने के लिए पायथन प्रोग्राम?

    मान लीजिए कि हमारे पास एक स्ट्रिंग और शब्द है और हमें अजगर का उपयोग करके इस शब्द की घटना की संख्या को हमारे स्ट्रिंग में खोजने की आवश्यकता है। इस खंड में हम यही करने जा रहे हैं, किसी दिए गए स्ट्रिंग में शब्द की संख्या गिनें और उसे प्रिंट करें। किसी दिए गए स्ट्रिंग में शब्दों की संख्या गिनें विधि 1

  1. एक टपल में एक तत्व की घटनाओं की गणना करने के लिए पायथन कार्यक्रम

    यहां एक उपयोगकर्ता इनपुट टपल दिया गया है, हमारा कार्य किसी दिए गए तत्व की घटनाओं को टपल में गिनना है। उदाहरण Input : A = [10, 20, 30, 40, 10, 100, 80, 10] X = 10 Output : 3 एल्गोरिदम countoccur(A,x) /* A is an array and x is the element to count the number of occurrences */ Step 1: First

  1. एक संख्या में कुल बिट्स गिनने के लिए एक पायथन प्रोग्राम लिखें?

    पहले हम एक नंबर इनपुट करते हैं फिर इस नंबर को बिन () फ़ंक्शन का उपयोग करके बाइनरी में परिवर्तित करते हैं और फिर आउटपुट स्ट्रिंग के पहले दो अक्षर 0b को हटाते हैं, फिर बाइनरी स्ट्रिंग की लंबाई की गणना करते हैं। उदाहरण Input:200 Output:8 स्पष्टीकरण Binary representation of 200 is 10010000 एल्गोरिद