-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring.f90
68 lines (51 loc) · 1.86 KB
/
string.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
!>
!! \brief This module contains routines for string manipulation
!!
!! \b Author: Garrelt Mellema
!!
!! \b Date: August 17, 1989
module string_manipulation
implicit none
contains
!------------------------------------------------------------------------------
!
! Program name:
!
! convert_case
!
! Purpose:
!
! This program shows how to use DO and CASE control constructs.
! It reads in a character string, changes the case of letters and
! writes out new string.
!
! Note:
!
! Need to know the difference in the collation sequence of the upper
! and lower case characters - use position of upper and lower case A:
!
! IACHAR('A') - IACHAR('a')
!
!------------------------------------------------------------------------------
!> This routine receives a character string, changes the case of letters and
!> returns the new string.
subroutine convert_case (string,direction)
CHARACTER (LEN = *),intent(inout) :: string !< input string
integer,intent(in) :: direction !< direction of conversion: 0 = upper to lower, 1 = lower to upper.
INTEGER :: i, upper_to_lower, len_string
upper_to_lower = IACHAR("a") - IACHAR("A")
! Find length of string excluding trailing blanks
len_string = LEN_TRIM(string)
DO i = 1, len_string
SELECT CASE (string(i:i))
CASE ('A':'Z') ! Upper case character found:
if (direction == 0) &
string(i:i) = ACHAR(IACHAR(string(i:i)) + upper_to_lower)
CASE ('a':'z') ! Lower case character found:
if (direction == 1) &
string(i:i) = ACHAR(IACHAR(string(i:i)) - upper_to_lower)
! No change for any other characters
END SELECT
END DO
END subroutine convert_case
end module string_manipulation