There are a number of character oriented intrinsic operations. Some of the basic character oriented functions include:
Function Description
ACHAR(I) Returns the character represented by integer argument I based on the ASCII table (Appendix A). Integer argument I must be between 1 and 127.
IACHAR(C) Returns the integer value of the character argument C represented by ASCII table (Appendix A).
LEN(STR) Returns an integer value representing the length of string
Chapter 11 ◄ Characters and Strings
argument STR.
LEN_TRIM(STR) Returns an integer value representing the length of string argument STR excluding any trailing spaces.
TRIM(STR) Returns string based on the string argument STR with any trailing spaces removed.
A complete list of intrinsic functions can be found in Appendix D.
11.10 Example
This example will scan a string and convert all lower-case letter to upper-case.
11.10.1 Understand the Problem
For this problem, the string will be read from the user with a maximum of 80 characters. Any lower case letters encountered will be converted to upper-case. All other characters (digits, symbols, etc.) will be left alone. To determine if a characters is lower-case, we can see if it is between “a” and “z”.
The final string will be displayed back to the screen. Based on the ASCII table in Appendix A, there is a specific, fixed difference between each upper and case letter. Thus, in order to convert a lower-case character to upper-lower-case, that difference can be subtracted. However, in order to perform the subtraction, each character needs to be converted into an integer (based on its value in the ASCII table).
The IACHAR() intrinsic function performs this conversion. After the conversion (subtraction), the integer must be converted back into its corresponding character, which can be accomplished with the ACHAR() intrinsic function. These functions work on a single character/integer, so each character will need to be addressed individually.
11.10.2 Create the Algorithm
For this problem, first we will need to prompt for and read the input string. Then any trailing blanks will be removed and the final length can be determined. Based on that length, each character will be accessed and converted if needed.
! declare variables
For convenience, the steps are written a program comments.
Chapter 11 ► Characters and Strings
11.10.3 Develop the Program
Based on the algorithm, the below program could be developed.
program caseconverter
! declare variables implicit none
integer :: i, strlen character(80) :: string1
!
! display initial header
write(*,'(a,/)') "Case Conversion Example"
!
! prompt for string
! read string
write(*,'(a)',advance="no") "Enter String (80 char max): "
read (*,'(a)') string1
!
! trim any trailing blanks
! determine length of string strlen = len(trim(string1))
! loop
do i = 1, strlen
! access each character
! if check lowercase > convert to uppercase
if ( string1(i:i) >= "a" .and. string1(i:i) <= "z" ) then string1(i:i) = achar( iachar(string1(i:i)) 32) end if
end do
!
! display final string
write(*,'(/,a)') ""
write(*,'(a,/,2x,a,/)') "Final String: ", string1 end program caseconverter
The spacing and indentation is not required, but helps to make the program more easily readable.
Chapter 11 ◄ Characters and Strings
11.10.4 Test/Debug the Program
For this problem, the testing would ensure that the output string is correct for the given input.
For example, the following output, c:\mydir> case
Case Conversion Example
Enter String (80 char max): Hello World!?
Final String:
HELLO WORLD!?
Each lower-case letter was converted to upper-case while the upper-case, space, and punctuation were unchanged. The program should be executed with a series of test inputs to verify the correct output.
11.11 Exercises
Below are some quiz questions and project suggestions based on this chapter.
11.11.1 Quiz Questions
Below are some quiz questions.
1) What is the declaration for a character variable, msg, to contain the string “Hello World!”?
2) Given the following relational expressions,
"D" > "c"
"100" < "20"
"Da" > "cA"
"20" < "10"
"d" > "C"
"20" < "100"
"ABBC" <= "ABCD"
state which will evaluate to true and which to false.
Chapter 11 ► Characters and Strings
3) Given the following Fortran statements,
character(len=*) :: str1="abcdef", str2="ABCDEF"
character(len=*) :: str3="123456", str4="78910"
character(len=12) :: astr1, astr2, astr3, astr4 astr1 = str1(1:3)
astr2 = str3(4:6) astr3 = str3 // str4
astr4 = str2(4:6) // str3(1:3) // str1(2:3) provide the resulting strings (astr1, astr2, astr3, and astr4).
4) How can the integer value (based on the ASCII table) of a character be obtained?
5) How can integer value be converted to a character (based on the ASCII table)?
11.11.2 Suggested Projects
Below are some suggested projects.
1) Type in the case conversion example program example, compile, and execute the program. Test the program on a series of different input values and verify that the output is correct for those input values.
2) Update the case conversion example program to convert any upper-case characters to lower-case characters. Test the program on a series of different input values and verify that the output is correct for those input values.
3) Write a program to read a string and count the vowels (“a”, “e”, “i”, “o”, and “u”). The program should provided a count for each vowel and a total count of vowels. The program should ensure that the vowels are counted for both upper and lower-case. Test the program on a series of different input values and verify that the output is correct for those input values.
4) Write a program to read 5 strings (≤ 80 characters each) and display the strings in alphabetical order. Test the program with a variety of different input strings, including digits, upper-case, and lower-case characters. Test the program on a series of different input values and verify that the output is correct for those input values.
12 File Operations
File operations allow Fortran programs to read from files and/or write to files. The basic read and write statements for file operations are the same as previously used with some additional information or clauses.