Answers for "counting number of words in a string"

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
1

how to count number of words in a string

String name = "Carmen is a fantastic play"; //arbitrary sentence
        
        int numWords = (name.split("\\s+")).length; //split string based on whitespace
                                                //split returns array - find legth of array
        
        System.out.println(numWords);
Posted by: Guest on June-06-2020
0

how to count words in string

String str = "I am happy and why not
  and why are you not happy and you should be";
        String [] arr = str.split(" ");
        Map<String, Integer> map = new HashMap<>();

        for (int i=0 ; i < arr.length ; i++){
                if (!map.containsKey(arr[i])){
                    map.put(arr[i],1);
                } else{
                    map.put(arr[i],map.get(arr[i])+1);
                }
        }
        for(Map.Entry<String, Integer> each : map.entrySet()){

  System.out.println(each.getKey()+" occures " + each.getValue() + " times");
        }
Posted by: Guest on January-22-2021
0

count words in string

// Count words (text separated by whitespace) in a piece of text
use std::collections::HashMap;

fn word_count(text: &str) -> HashMap<&str, i32> {
    let mut map = HashMap::new();
    for word in text.split_whitespace() {
        *map.entry(word).or_insert(0) += 1;
    }
    map
}

fn main() {
    println!("Count of words = {:?} ",word_count("the quick brown fox jumped over the lazy dog"));
}
Posted by: Guest on August-14-2021
0

program code for counting the similarwrod in the sentences

public static int count(String word) {
    if (word == null || word.isEmpty()) {
      return 0;
    }

    int wordCount = 0;

    boolean isWord = false;
    int endOfLine = word.length() - 1;
    char[] characters = word.toCharArray();

    for (int i = 0; i < characters.length; i++) {

      // if the char is a letter, word = true.
      if (Character.isLetter(characters[i]) && i != endOfLine) {
        isWord = true;

        // if char isn't a letter and there have been letters before,
        // counter goes up.
      } else if (!Character.isLetter(characters[i]) && isWord) {
        wordCount++;
        isWord = false;

        // last word of String; if it doesn't end with a non letter, it
        // wouldn't count without this.
      } else if (Character.isLetter(characters[i]) && i == endOfLine) {
        wordCount++;
      }
    }

    return wordCount;
  }
Posted by: Guest on October-31-2020

Code answers related to "counting number of words in a string"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language