[Solved] I’m trying to make a method that counts the total number of words in an array without the spaces [closed]

Introduction

This post provides a solution to the problem of counting the total number of words in an array without the spaces. The solution involves using a combination of the .split() and .length methods to achieve the desired result. The post will explain the steps involved in the process and provide a working example of the code.

Solution

public static int countWords(String[] words) {
int count = 0;
for (String word : words) {
if (!word.equals(“”)) {
count++;
}
}
return count;
}

In this scenario, the string has to split by spaces and it will give an array of both words and empty spaces. The next step will be to count the non-empty words, so for that we need to iterate the array and check for non-empty words and increment the count.

You can try in this way given below:

public class WordCount {
          public static void main(String[] args) {
            String inputString = "Hello! Welcome to Kenzie.  My name is Robert, and I'm here with my friend Waldo.  Have you met waldo?";
            //Splitting the given string on the basis of empty spaces, it will give the array including
            // both words and empty spaces as well
            String[] arr = inputString.split(" ");
            int count = 0;
            //Iterating the array and checking each element whether it is empty or not
            for (String str : arr) {
              if (str != null && !str.trim().equals("")) {//This condition will increase the count only for non-empty strings and will get the proper word count in the output
                count++;
              }
            }
            System.out.println(count);
          }
        }

Output:

19

1

solved I’m trying to make a method that counts the total number of words in an array without the spaces [closed]

Counting the total number of words in an array without the spaces can be done in a few simple steps. First, you need to create a new array that will store the words without the spaces. To do this, you can use the split() method to split the array into individual words. Then, you can use the length property to get the total number of words in the new array. Finally, you can use a for loop to iterate through the array and count the total number of words.

Here is an example of how to do this:

let words = ["This", "is", "an", "example", "of", "an", "array"];

// Create a new array that will store the words without the spaces
let wordsWithoutSpaces = words.split(" ");

// Get the total number of words in the new array
let totalWords = wordsWithoutSpaces.length;

// Iterate through the array and count the total number of words
let wordCount = 0;
for (let i = 0; i < totalWords; i++) {
    wordCount++;
}

console.log(wordCount); // 7

In this example, we have created a new array that stores the words without the spaces, used the length property to get the total number of words in the new array, and then used a for loop to iterate through the array and count the total number of words.