Answers for "how to count number of words in python"

1

Count number of words in a String

public static void main (String[] args) {

     System.out.println("Simple Java Word Count Program");

     String str1 = "Today is Holdiay Day";

     String[] wordArray = str1.trim().split("\\s+");
     int wordCount = wordArray.length;

     System.out.println("Word count is = " + wordCount);
}
Posted by: Guest on August-14-2021
0

count words in string python

import string 

# sentence = ""
sentence = str(input("Enter here: "))

# Remove all punctuations
sentence = sentence.translate(str.maketrans('', '', string.punctuation))

# Remove all numbers"
sentence = ''.join([Word for Word in sentence if not Word.isdigit()])

count = 0;

for index in range(len(sentence)-1) :
    if sentence[index+1].isspace() and not sentence[index].isspace():
        count += 1 
        
print(count)
Posted by: Guest on May-31-2021

Code answers related to "how to count number of words in python"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language