Count Words in a Sentence

Easy Strings
Count the number of words in a sentence (words separated by spaces).

Sample input

the quick brown fox

Sample output

4

Solution

s = "the quick brown fox"
print(len(s.split()))
const s = "the quick brown fox";
console.log(s.trim().split(/\s+/).length);
public class Main {
    public static void main(String[] args) {
        String s = "the quick brown fox";
        System.out.println(s.trim().split("\\s+").length);
    }
}
fun main() {
    val s = "the quick brown fox"
    println(s.trim().split(Regex("\\s+")).size)
}
let s = "the quick brown fox"
let words = s.split(separator: " ")
print(words.count)
void main() {
  String s = 'the quick brown fox';
  print(s.trim().split(RegExp(r'\s+')).length);
}
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    string s = "the quick brown fox";
    stringstream ss(s);
    string word;
    int count = 0;
    while (ss >> word) count++;
    cout << count << endl;
    return 0;
}
#include <stdio.h>
#include <ctype.h>

int main() {
    char s[] = "the quick brown fox";
    int count = 0, inWord = 0;
    for (int i = 0; s[i]; i++) {
        if (!isspace(s[i]) && !inWord) {
            inWord = 1;
            count++;
        } else if (isspace(s[i])) {
            inWord = 0;
        }
    }
    printf("%d\n", count);
    return 0;
}