|
1 | | -! upcase and to_upper didn't work, |
2 | | -! had to resort to check ASCII value of first letter & then |
3 | | -! subtract 32 from it, ... |
4 | 1 | program capitalize |
5 | | -character(len=100) :: cmd |
6 | | -character(len=1) :: firstletter |
7 | | -character(len=:), allocatable :: printoutput |
8 | | - |
9 | | -! Anything not equal to single argument, Print Error |
10 | | -IF(COMMAND_ARGUMENT_COUNT().NE.1)THEN |
11 | | - write(*,'(g0.8)')"Usage: please provide a string" |
12 | | - STOP |
13 | | -ENDIF |
14 | | - |
15 | | -CALL GET_COMMAND_ARGUMENT(1,cmd) |
16 | | -if (cmd == "") then |
17 | | - write(*,'(g0.8)')"Usage: please provide a string" |
18 | | - STOP |
19 | | -endif |
20 | | -! Get first letter |
21 | | - firstletter = cmd(1:1) |
22 | | - ! Check if first letter is between ASCII Value of a and z |
23 | | - if (iachar(firstletter)>= iachar("a") .and. iachar(firstletter)<=iachar("z") ) then |
24 | | - ! Subtract 32 from ASCII Value, to convert it to respective capital letter |
25 | | - firstletter = achar(iachar(firstletter)-32) |
26 | | -! Overwrite the first letter |
27 | | - cmd(1:1) = firstletter |
28 | | - end if |
29 | | - printoutput = adjustl(trim(cmd)) |
30 | | - write(*,'(g0.8)')printoutput |
| 2 | + implicit none |
| 3 | + character(len=100) :: cmd |
| 4 | + integer :: argc |
| 5 | + |
| 6 | + argc = command_argument_count() |
| 7 | + if (argc /= 1) call usage() |
| 8 | + |
| 9 | + call get_command_argument(1, cmd) |
| 10 | + cmd = adjustl(trim(cmd)) |
| 11 | + if (len_trim(cmd) == 0) call usage() |
| 12 | + |
| 13 | + cmd(1:1) = upper(cmd(1:1)) |
| 14 | + |
| 15 | + write(*,*) cmd |
| 16 | + |
| 17 | +contains |
| 18 | + |
| 19 | + subroutine usage() |
| 20 | + write(*,*) "Usage: please provide a string" |
| 21 | + stop |
| 22 | + end subroutine |
| 23 | + |
| 24 | + pure elemental function upper(str) result(string) |
| 25 | + character(*), intent(in) :: str |
| 26 | + character(len(str)) :: string |
| 27 | + integer :: i, iend |
| 28 | + integer, parameter :: toupper = iachar('A') - iachar('a') |
| 29 | + |
| 30 | + iend = len_trim(str) |
| 31 | + string = str(:iend) |
| 32 | + |
| 33 | + do concurrent (i = 1:iend) |
| 34 | + select case (str(i:i)) |
| 35 | + case ('a':'z') |
| 36 | + string(i:i) = achar(iachar(str(i:i)) + toupper) |
| 37 | + end select |
| 38 | + end do |
| 39 | + end function upper |
| 40 | + |
31 | 41 | end program capitalize |
0 commit comments