In this article we will learn about some of the frequently asked about Java programming questions in technical like “count words in string” Code Answer’s. When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. Error or stack handling on Java was simple and easy. An error message with filename, line number and a message describing the error is sent to the browser. This tutorial contains some of the most common error checking methods in Java. Below are some solution about “count words in string” Code Answer’s.
Count number of words in a String
xxxxxxxxxx
1
public static void main (String[] args) {
2
3
System.out.println("Simple Java Word Count Program");
4
5
String str1 = "Today is Holdiay Day";
6
7
String[] wordArray = str1.trim().split("\s+");
8
int wordCount = wordArray.length;
9
10
System.out.println("Word count is = " + wordCount);
11
}
12
count word in string no matter the delimiter java
xxxxxxxxxx
1
public static void main(String[] args)
2
{
3
//Scanner object instantiation
4
Scanner dude = new Scanner(System.in);
5
6
//variable declaration
7
String string1 = "";
8
int count = 0;
9
boolean isWord = false;
10
11
12
//user prompt and input
13
System.out.println("Enter in your string");
14
string1 = dude.nextLine();
15
16
int endOfLine = string1.length()-1;
17
char ch [] = string1.toCharArray();
18
19
for (int i = 0; i < string1.length(); i++)
20
{
21
if(Character.isLetter(ch[i]) && i != endOfLine)
22
{//if character is letter and not end of line
23
isWord = true; //it is part of a word
24
}
25
if (!Character.isLetter(ch[i]) && isWord)
26
{ //if character is not a letter, and previous
27
//character is a letter i.e. non-letter is
28
//preceded by character
29
count++; //add to word count
30
isWord = false; //get ready to detect new word
31
}
32
if (Character.isLetter(ch[i]) && i == endOfLine)
33
{ //if character is letter
34
//and at end of line
35
count++; //add to word count
36
isWord = false;
37
}
38
39
}
40
System.out.println("There are " +count+ " words");
41
}
count words in string python
xxxxxxxxxx
1
import string
2
3
# sentence = ""
4
sentence = str(input("Enter here: "))
5
6
# Remove all punctuations
7
sentence = sentence.translate(str.maketrans('', '', string.punctuation))
8
9
# Remove all numbers"
10
sentence = ''.join([Word for Word in sentence if not Word.isdigit()])
11
12
count = 0;
13
14
for index in range(len(sentence)-1) :
15
if sentence[index+1].isspace() and not sentence[index].isspace():
16
count += 1
17
18
print(count)
how to count words in string
xxxxxxxxxx
1
String str = "I am happy and why not
2
and why are you not happy and you should be";
3
String [] arr = str.split(" ");
4
Map<String, Integer> map = new HashMap<>();
5
6
for (int i=0 ; i < arr.length ; i++){
7
if (!map.containsKey(arr[i])){
8
map.put(arr[i],1);
9
} else{
10
map.put(arr[i],map.get(arr[i])+1);
11
}
12
}
13
for(Map.Entry<String, Integer> each : map.entrySet()){
14
15
System.out.println(each.getKey()+" occures " + each.getValue() + " times");
16
}
count words in string
xxxxxxxxxx
1
// Count words (text separated by whitespace) in a piece of text
2
use std::collections::HashMap;
3
4
fn word_count(text: &str) -> HashMap<&str, i32> {
5
let mut map = HashMap::new();
6
for word in text.split_whitespace() {
7
*map.entry(word).or_insert(0) += 1;
8
}
9
map
10
}
11
12
fn main() {
13
println!("Count of words = {:?} ",word_count("the quick brown fox jumped over the lazy dog"));
14
}