25.06.2013 Views

Il Linguaggio Fortran 90/95

Il Linguaggio Fortran 90/95

Il Linguaggio Fortran 90/95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

40 Tipi ed espressioni<br />

Si noti che una sottostringa fa riferimento alle stesse locazioni di memoria della stringa<br />

madre per cui se viene modificato il contenuto della sottostringa cambiano automaticamente<br />

anche i caratteri della stringa madre.<br />

Come detto in precedenza, una sottostringa può essere usata come una qualunque variabile<br />

stringa ”originale” per cui, in particolare, essa può essere usata a sinistra di una espressione di<br />

assegnazione. Quanto detto è ben illustrato dal seguente programma:<br />

PROGRAM test_sottostringhe<br />

IMPLICIT NONE<br />

CHARACTER(LEN=6) :: str1, str2, str3<br />

str1 = "abcdef"<br />

str2 = "123456"<br />

str3 = str1(3:5)<br />

str2(5:6) = str1(1:2)<br />

PRINT*, str1<br />

PRINT*, str2<br />

PRINT*, str3<br />

END PROGRAM test_sottostringhe<br />

il cui output è:<br />

abcdef<br />

1234ab<br />

cde<br />

Operatore di concatenazione di stringhe<br />

L’operatore di concatenazione di stringhe combina due o più stringhe (o sottostringhe) a formare<br />

un’unica stringa più grande. Esso è rappresentato dal simbolo di un doppio slash (”//”). La<br />

forma generale di questo operatore è la seguente:<br />

stringa_1//stringa_2 [// stringa_3 ...]<br />

in cui, se le stringhe stringa_1 e stringa_2 hanno lunghezza n_1 ed n_2 , rispettivamente,<br />

la lunghezza della stringa finale sarà n_1 +n_2 .<br />

Si consideri il seguente frammento di programma:<br />

CHARACTER(LEN=7) :: nome1="Donald"<br />

CHARACTER(LEN=5) :: nome2="Duck", nome3="Duffy"<br />

CHARACTER(LEN=11) :: ans1, ans2, ans3<br />

...<br />

ans1 = nome1//nome2<br />

ans2 = nome3//" "//nome2<br />

ans3 = nome2//nome2//nome2<br />

A seguito delle operazioni di concatenazione, il contenuto delle variabili ans1, ans2, ans3, sarà<br />

il seguente:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!