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 "!@#$%^&*()_+-=',.: "
.s
is ' '
.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 "' ]
temp.f95:34:32: 34 | '"Hello, my name is John "', & | 1 Error: Different CHARACTER lengths (25/8) in array constructor at (1) temp.f95:8:14: 8 | read (examples(i), *) s | 1 Error: Function โexamplesโ at (1) has no IMPLICIT type temp.f95:7:27: 7 | do i = 1, size(examples) | 1 Error: Symbol โexamplesโ at (1) has no IMPLICIT type
! 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
At line 13 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7b55699a2960 in ??? #1 0x7b55699a34d9 in ??? #2 0x7b5569bf717b in ??? #3 0x7b5569bf0684 in ??? #4 0x7b5569bf12aa in ??? #5 0x5b5bcc20f21c in MAIN__ #6 0x5b5bcc20f31e in main
def count_segments(s: str) -> int:
return len(s.split())
The algorithm goes as follows:
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.
#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:
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.