Characters
February 2020
Back to index.html.
Characters
In programming, a sequence of characters is called a string(s). Also, a part of the sequence is substring(s). This chapter introduces some features to manipulate strings.
Some operators for characters
Concatinating characters
The //
symbol concatenates two character-literals.
program ch
print *,"Hello, " // "world!"
end program ch
You can use it for character variables.
program ch
implicit none
character(len=10) :: x,y
x = "Hello, "
y = "world!"
print *,x // y
end program ch
Hello, world!
It seems wrong. Each variable has 10-character length, and print
keeps the margin enough to show the full width of each variable. In the above case, print
statement shows Hello,
followed by padded spaces (as 4 characters). To avoid it, you cut the trailed spaces with the intrinsic function, trim
.
! for exercise 1
print *,trim(x) // y
Hello,world!
But it removes all the trailing spaces in x
while keeping the other spaces. The output has no space between the words. Exercise 1 asks you how you show Hello, world!
in the right way.
Character functions
There are many built-in functions useful for manipulating characters.
Function | Effect |
---|---|
len(x) |
the defined length of x |
len_trim(x) |
the length of x excluding the training spaces |
adjustl(x) |
left alignment |
adjustr(x) |
right alignment |
trim(x) |
removing the trailing spaces |
See the following example.
! for excercise 1
program ch
implicit none
character(len=10) :: x
x = ' 123 45 '
print *,len(x)
print *,len_trim(x)
print *,adjustl(x)
print *,adjustr(x)
print *,trim(x)
end program ch
10
8
123 45
123 45
123 45
To remove the leading spaces, you apply adjust()
to the variable.
x = adjustl(x)
Some other functions may be useful to manipulate the characters precisely.
Function | Effect |
---|---|
index(x,y) |
stating position of y in x , or 0 if not found |
scan(x,s) |
position of one of s in x , or 0 if not found |
verify(x,s) |
position of none of s in x , or 0 if found |
index(x,y,back=.true.) |
same as before but searching backword |
scan(x,s,back=.true.) |
same as before but searching backword |
verify(x,s,back=.true.) |
same as before but searching backword |
Here are some examples of these functions.
program ch
print *,index("1231234512","123") ! 1
print *,index("1231234512","234") ! 5
print *,scan("1231234512","1234") ! 1
print *,verify("1231234512","1234") ! 8
print *,index("1231234512","123",back=.true.) ! 4
print *,scan("1231234512","1234",back=.true.) ! 10
print *,verify("1231234512","1234",back=.true.) ! 8
end program ch
Alternative literal symbols
The character literals are defined by '
or "
. If the literal contains one of such characters, you should use the alternative one.
program ch
! shows: It's my dog
print *,"It's my dog"
! shows: You said "I was not there".
print *,'You said "I was not there".'
end program ch
When you have both '
and "
in the literal, it looks more complicated.
program ch
! shows: You said "it's my dog".
print *,"You said ",'"',"it's my dog.",'"'
! the same result
print *,"You said " // '"' // "it's my dog." // '"'
end program ch
Reading characters from keyboard
By default, the input characters are split by white spaces into pieces. It is useful to read some numbers, but you can not read the entire input with spaces from the keyboard.
program ch
implicit none
character(len=10) :: x
read *,x
print *,x
end program ch
When you type abc def
in the terminal, you should find the following output.
abc
You can read the entire characters with spaces using the format identifier. We do not see it here, and the later chapter deals with it.
Summary
- Characters can be concatenated with
//
. - There are several functions to manipulate characters.
- With a naive approach, you can not read characters with spaces from the keyboard.
Exercises
See the above code.
- Show
Hello, world!
in the right way (with a space between the words). - Show the length of x, excluding all leading and training spaces. In this case, make the program show
6
forx=" 123 45 "
.
Back to index.html.