Given a string s, return the number of segments in the string.

A segment is defined to be a contiguous sequence of non-space characters.

Example 1:

Input: s = "Hello, my name is John " Output: 5 Explanation: The five segments are [ "Hello, ", "my ", "name ", "is ", "John "]

Example 2:

Input: s = "Hello " Output: 1

Constraints:

  • 0 <= s.length <= 300
  • s consists of lowercase and uppercase English letters, digits, or one of the following characters "!@#$%^&*()_+-=',.: ".
  • The only space character in s is ' '.

Note: This problem is from LeetCode.
โŒ Compiled
โŒ Executed
โŒ Correct
program main
    implicit none

    integer :: i, n
    character(len=300) :: s

    do i = 1, size(examples)
        read (examples(i), *) s
        write (*,*) 'Example ', i, ':'
        write (*,*) 'Input: ', trim(s)
        n = count_segments(s)
        write (*,*) 'Output: ', n
        write (*,*) ''
    end do

contains

    function count_segments(s) result(n)
        implicit none
        character(*), intent(in) :: s
        integer :: n, i

        n = 0
        do i = 1, len(s)
            if (s(i:i) /= ' ') then
                n = n + 1
            end if
        end do
    end function count_segments

end program main

character(len=*), parameter :: examples(2) = [ &
    '"Hello, my name is John "', &
    '"Hello "' ]
โœ… Compiled
โŒ Executed
โŒ Correct
! This is a full Fortran script with a valid main entry point that solves the problem
! of counting the number of segments in a string.

program count_segments

implicit none

! Declare the variables
character(len=300) :: s
integer :: i, j, n_segments

! Read the input string from the command line
read (*,*) s

! Initialize the number of segments to 0
n_segments = 0

! Loop through the characters of the string
do i = 1, len_trim(s)

    ! Check if the current character is a space
    if (s(i:i) == ' ') then

        ! If it is, increment the number of segments
        n_segments = n_segments + 1

    end if

end do

! Output the number of segments to stdout
write (*,*) n_segments

end program count_segments
๐ŸŒ Data from online sources
def count_segments(s: str) -> int:
    return len(s.split())

The algorithm goes as follows:

  1. Remove leading and trailing spaces from the input string.
  2. Split the string on any whitespace into an array or list.
  3. Return the length of the resulting array or list, which corresponds to the number of segments in the input string.

In C++, the std::stringstream is used to read words separated by spaces with the >> operator. Java and JavaScript use a regex to match any whitespace, and Python has a built-in split() method that splits on any whitespace by default.

๐ŸŒ Data from online sources
#include <string>
#include <sstream>

int countSegments(const std::string& s) {
    std::stringstream ss(s);
    int count = 0;
    std::string temp;

    while (ss >> temp) {
        ++count;
    }

    return count;
}

The algorithm goes as follows:

  1. Remove leading and trailing spaces from the input string.
  2. Split the string on any whitespace into an array or list.
  3. Return the length of the resulting array or list, which corresponds to the number of segments in the input string.

In C++, the std::stringstream is used to read words separated by spaces with the >> operator. Java and JavaScript use a regex to match any whitespace, and Python has a built-in split() method that splits on any whitespace by default.