diff --git a/src/stdlib_ascii.f90 b/src/stdlib_ascii.f90 index e446f29e2..38976b726 100644 --- a/src/stdlib_ascii.f90 +++ b/src/stdlib_ascii.f90 @@ -277,23 +277,30 @@ end function to_upper pure function to_title(string) result(title_string) character(len=*), intent(in) :: string character(len=len(string)) :: title_string + logical:: capitalize_flag integer :: i, n n = len(string) + capitalize_flag = .TRUE. do i = 1, len(string) - if (is_alphanum(string(i:i))) then - title_string(i:i) = char_to_upper(string(i:i)) - n = i - exit + + if(capitalize_flag) then + if (is_alphanum(string(i:i))) then + title_string(i:i) = char_to_upper(string(i:i)) + capitalize_flag = .FALSE. + else + title_string(i:i) = char_to_lower(string(i:i)) + end if else - title_string(i:i) = string(i:i) + if(string(i:i)==" ") then + title_string(i:i) = char_to_lower(string(i:i)) + capitalize_flag = .TRUE. + else + title_string(i:i) = char_to_lower(string(i:i)) + end if end if end do - do i = n + 1, len(string) - title_string(i:i) = char_to_lower(string(i:i)) - end do - end function to_title !> Reverse the character order in the input character variable diff --git a/src/tests/ascii/test_ascii.f90 b/src/tests/ascii/test_ascii.f90 index cfe4a938c..ca1d5ddc5 100644 --- a/src/tests/ascii/test_ascii.f90 +++ b/src/tests/ascii/test_ascii.f90 @@ -590,7 +590,7 @@ subroutine test_to_title_string call check(trim(dlc) == "Title") dlc = to_title(" s P a C e D !") - call check(dlc == " S p a c e d !") + call check(dlc == " S P A C E D !") dlc = to_title("1st, 2nd, 3rd") call check(dlc == "1st, 2nd, 3rd") diff --git a/src/tests/string/test_string_functions.f90 b/src/tests/string/test_string_functions.f90 index 4a8d516a8..3da7e1f67 100644 --- a/src/tests/string/test_string_functions.f90 +++ b/src/tests/string/test_string_functions.f90 @@ -28,7 +28,7 @@ end subroutine test_to_upper_string subroutine test_to_title_string type(string_type) :: test_string, compare_string test_string = "_#To tiTlE !$%-az09AZ" - compare_string = "_#To title !$%-az09az" + compare_string = "_#To Title !$%-Az09az" call check(to_title(test_string) == compare_string)