Answers for "how do you count the 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
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

Code answers related to "how do you count the words in a string"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language