tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
16
Task/Reverse-a-string/Fortran/reverse-a-string-1.f
Normal file
16
Task/Reverse-a-string/Fortran/reverse-a-string-1.f
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
PROGRAM Example
|
||||
|
||||
CHARACTER(80) :: str = "This is a string"
|
||||
CHARACTER :: temp
|
||||
INTEGER :: i, length
|
||||
|
||||
WRITE (*,*) str
|
||||
length = LEN_TRIM(str) ! Ignores trailing blanks. Use LEN(str) to reverse those as well
|
||||
DO i = 1, length/2
|
||||
temp = str(i:i)
|
||||
str(i:i) = str(length+1-i:length+1-i)
|
||||
str(length+1-i:length+1-i) = temp
|
||||
END DO
|
||||
WRITE(*,*) str
|
||||
|
||||
END PROGRAM Example
|
||||
25
Task/Reverse-a-string/Fortran/reverse-a-string-2.f
Normal file
25
Task/Reverse-a-string/Fortran/reverse-a-string-2.f
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
program reverse_string
|
||||
|
||||
implicit none
|
||||
character (*), parameter :: string = 'no devil lived on'
|
||||
|
||||
write (*, '(a)') string
|
||||
write (*, '(a)') reverse (string)
|
||||
|
||||
contains
|
||||
|
||||
recursive function reverse (string) result (res)
|
||||
|
||||
implicit none
|
||||
character (*), intent (in) :: string
|
||||
character (len (string)) :: res
|
||||
|
||||
if (len (string) == 0) then
|
||||
res = ''
|
||||
else
|
||||
res = string (len (string) :) // reverse (string (: len (string) - 1))
|
||||
end if
|
||||
|
||||
end function reverse
|
||||
|
||||
end program reverse_string
|
||||
Loading…
Add table
Add a link
Reference in a new issue