From 4119693a2678c7a867883bdf4c9707c06b4bfbf9 Mon Sep 17 00:00:00 2001 From: ChetanKarwa Date: Wed, 31 Mar 2021 01:46:46 +0530 Subject: [PATCH 1/2] To_Title is corrected --- src/stdlib_ascii.f90 | 25 ++++++++++++++-------- src/tests/ascii/test_ascii.f90 | 2 +- src/tests/string/test_string_functions.f90 | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/stdlib_ascii.f90 b/src/stdlib_ascii.f90 index e446f29e2..0e70c0274 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) = string(i:i) + end if else - title_string(i:i) = string(i:i) + if(string(i:i)==" ") then + title_string(i:i) = string(i:i) + capitalize_flag = .TRUE. + else + title_string(i:i) = 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) From fac5239416b90ee37c7e3b51f3d7eccda3c2344f Mon Sep 17 00:00:00 2001 From: ChetanKarwa Date: Wed, 31 Mar 2021 02:11:58 +0530 Subject: [PATCH 2/2] to_title corrected --- src/stdlib_ascii.f90 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stdlib_ascii.f90 b/src/stdlib_ascii.f90 index 0e70c0274..38976b726 100644 --- a/src/stdlib_ascii.f90 +++ b/src/stdlib_ascii.f90 @@ -289,14 +289,14 @@ pure function to_title(string) result(title_string) title_string(i:i) = char_to_upper(string(i:i)) capitalize_flag = .FALSE. else - title_string(i:i) = string(i:i) + title_string(i:i) = char_to_lower(string(i:i)) end if else if(string(i:i)==" ") then - title_string(i:i) = string(i:i) + title_string(i:i) = char_to_lower(string(i:i)) capitalize_flag = .TRUE. else - title_string(i:i) = string(i:i) + title_string(i:i) = char_to_lower(string(i:i)) end if end if end do