📜  Fortran-字符

📅  最后修改于: 2020-11-04 06:15:57             🧑  作者: Mango


Fortran语言可以将字符视为单个字符或连续的字符串。

字符可以是取自基本字符集的任何符号,即字母,十进制数字,下划线和21个特殊字符。

字符常量是一个固定的值的字符串。

固有数据类型字符存储字符和字符串。字符串的长度可以由len说明符指定。如果未指定长度,则为1。可以按位置引用字符串的各个字符;否则,可以为0。最左边的字符在位置1。

字符宣言

声明字符类型数据与其他变量相同-

type-specifier :: variable_name

例如,

character :: reply, sex

您可以分配一个值,例如

reply = ‘N’ 
sex = ‘F’

以下示例演示了字符数据类型的声明和使用-

program hello
implicit none

   character(len = 15) :: surname, firstname 
   character(len = 6) :: title 
   character(len = 25)::greetings
   
   title = 'Mr. ' 
   firstname = 'Rowan ' 
   surname = 'Atkinson'
   greetings = 'A big hello from Mr. Bean'
   
   print *, 'Here is ', title, firstname, surname
   print *, greetings
   
end program hello

当您编译并执行上述程序时,它将产生以下结果-

Here is Mr. Rowan Atkinson       
A big hello from Mr. Bean

字符的级联

连接运算符// //连接字符。

以下示例演示了这一点-

program hello
implicit none

   character(len = 15) :: surname, firstname 
   character(len = 6) :: title 
   character(len = 40):: name
   character(len = 25)::greetings
   
   title = 'Mr. ' 
   firstname = 'Rowan ' 
   surname = 'Atkinson'
   
   name = title//firstname//surname
   greetings = 'A big hello from Mr. Bean'
   
   print *, 'Here is ', name
   print *, greetings
   
end program hello

当您编译并执行上述程序时,它将产生以下结果-

Here is Mr.Rowan Atkinson       
A big hello from Mr.Bean

一些字符功能

下表显示了一些常用的字符函数以及说明-

Sr.No Function & Description
1

len(string)

It returns the length of a character string

2

index(string,sustring)

It finds the location of a substring in another string, returns 0 if not found.

3

achar(int)

It converts an integer into a character

4

iachar(c)

It converts a character into an integer

5

trim(string)

It returns the string with the trailing blanks removed.

6

scan(string, chars)

It searches the “string” from left to right (unless back=.true.) for the first occurrence of any character contained in “chars”. It returns an integer giving the position of that character, or zero if none of the characters in “chars” have been found.

7

verify(string, chars)

It scans the “string” from left to right (unless back=.true.) for the first occurrence of any character not contained in “chars”. It returns an integer giving the position of that character, or zero if only the characters in “chars” have been found

8

adjustl(string)

It left justifies characters contained in the “string”

9

adjustr(string)

It right justifies characters contained in the “string”

10

len_trim(string)

It returns an integer equal to the length of “string” (len(string)) minus the number of trailing blanks

11

repeat(string,ncopy)

It returns a string with length equal to “ncopy” times the length of “string”, and containing “ncopy” concatenated copies of “string”

例子1

这个例子展示了索引函数的使用-

program testingChars
implicit none

   character (80) :: text 
   integer :: i 
   
   text = 'The intrinsic data type character stores characters and   strings.'
   i=index(text,'character') 
   
   if (i /= 0) then
      print *, ' The word character found at position ',i 
      print *, ' in text: ', text 
   end if
   
end program testingChars

当您编译并执行上述程序时,它将产生以下结果-

The word character found at position 25
in text : The intrinsic data type character stores characters and strings.  

例子2

这个例子演示了trim函数的使用-

program hello
implicit none

   character(len = 15) :: surname, firstname 
   character(len = 6) :: title 
   character(len = 25)::greetings
   
   title = 'Mr.' 
   firstname = 'Rowan' 
   surname = 'Atkinson'
   
   print *, 'Here is', title, firstname, surname
   print *, 'Here is', trim(title),' ',trim(firstname),' ', trim(surname)
   
end program hello

当您编译并执行上述程序时,它将产生以下结果-

Here isMr.   Rowan          Atkinson       
 Here isMr. Rowan Atkinson

例子3

这个例子演示了achar函数的使用-

program testingChars
implicit none

   character:: ch
   integer:: i
   
   do i = 65, 90
      ch = achar(i)
      print*, i, ' ', ch
   end do
   
end program testingChars

当您编译并执行上述程序时,它将产生以下结果-

65  A
66  B
67  C
68  D
69  E
70  F
71  G
72  H
73  I
74  J
75  K
76  L
77  M
78  N
79  O
80  P
81  Q
82  R
83  S
84  T
85  U
86  V
87  W
88  X
89  Y
90  Z

检查字符的词汇顺序

以下函数确定字符的词法顺序-

Sr.No Function & Description
1

lle(char, char)

Compares whether the first character is lexically less than or equal to the second

2

lge(char, char)

Compares whether the first character is lexically greater than or equal to the second

3

lgt(char, char)

Compares whether the first character is lexically greater than the second

4

llt(char, char)

Compares whether the first character is lexically less than the second

例子4

以下函数演示了用法-

program testingChars
implicit none

   character:: a, b, c
   a = 'A'
   b = 'a'
   c = 'B'
   
   if(lgt(a,b)) then
      print *, 'A is lexically greater than a'
   else
      print *, 'a is lexically greater than A'
   end if
   
   if(lgt(a,c)) then
      print *, 'A is lexically greater than B'
   else
      print *, 'B is lexically greater than A'
   end if  
   
   if(llt(a,b)) then
      print *, 'A is lexically less than a'
   end if
   
   if(llt(a,c)) then
      print *, 'A is lexically less than B'
   end if
   
end program testingChars

当您编译并执行上述程序时,它将产生以下结果-

a is lexically greater than A
B is lexically greater than A
A is lexically less than a
A is lexically less than B