The bug:
You defined your string as an array of single characters of length 10:
character :: string(10)
A character string of length 10 is defined as
character(len=10) :: string
These two are two different concepts. See:
Why do I get 10 zeros?
The reason you obtained 10 times the value zero is because both INDEX
and SCAN
are elemental functions. This means that they execute for every element of the array. But this is not all, you would imagine that it would at least return ones the number 1 for the third array ellement. Unfortuntately, the assignment
character array(10)
array = "abcdefghik"
print *, array
will assign 10 times the letter a
. You notice that this will return aaaaaaaaaa
. This is due to array assignments.
Working code:
program test
implicit none
character(len=10) :: string;
string="abcdefghik"
print *, index(string, "c")
end program test