# LAB #5

## Objectives

• Practice more IF statements and loops
• Learn about strings

1. Type the following program exactly as shown:
```program Lab5
implicit none
! This program introduces Fortran string handling capabilities

character*26 upper, lower, name, cap
character str*2, one*1
integer*2 from, to, i, m

lower = "abcdefghijklmnopqrstuvwxyz"
upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
name = "York University"

! ----------------------------------------------------------Substrings:
do i = 1, 5
print*, "The string we are studying is: ", name
print*, "Enter two integers ..."
print*, name(from : to)
end do

! ----------------------------------------------------------Pattern:
do i = 1, 5
print*, "Enter any 2-character string (e.g. enter er) ..."
print*, index(name, str)
end do
5 format(A)

! ----------------------------------------------------------Capitalize:
do i = 1, len(name)
one = name(i : i)
m = index(lower, one)
if (m .ge. 1 .and. m .le. 26) then
one = upper(m : m)
end if
cap(i : i) = one
end do
print*, cap

! ----------------------------------------------------------Frequency:
print*, "Enter any single character to count its occurrences in ", name

end
```
2. Compile the program as you learned in Lab1.

## Questions

1. The program starts by displaying a string and then asks you to supply two integers. Based on their values, a string is displayed. The process is repeated 5 times so that you can recognize what these two integers mean and what syntax is needed.
2. Also repeating 5 times, the program asks you to enter a string of two characters and, based on it, it prints out an integer. Experiment, and look at the code, until you understand what's hapenning.
3. Based on your understanding of the above two points, you should be able to see how the string was capitalized.
4. You need to complete this part: The user enters a single character. Your job is to count how many times does this character appear in the string whose variable name is name. Do this by using a loop which examines each character in the string and increments a counter whenever a match is found.
5. Repeat the last task but use the index function instead. You still need a loop but your scan is much faster now since you don't have to explicitly examine each character.

Fall2002/HR