LAB #5


Objectives


Part A

Tasks

  1. Type the following program exactly as shown:
    program Lab5A
    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 ..."
       read*, from, to
       print*, name(from : to)
    end do
    
    ! ----------------------------------------------------------Pattern:
    do i = 1, 5
       print*, "Enter any 2-character string (e.g. enter er) ..."
       read 5, str
       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
    read 5, one
    
    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 happening.
  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.

Part B

Problem: Write a Fortran program to format a text file

The program will first input an integer which specifies the width of the output page. It will then input one word at a time and build a line by placing the word at the end of a growing line as long as it doesn't exceed the page width. If it goes over, the line is printed and the word becomes the first word on a new line.
  1. The following program contains the basic solution to the problem, but does not work properly because of the funny way in which Fortran handles strings. Try it and see what happens.
    program Lab5B
    implicit none
    
    integer pageWidth
    character*78 line, word
    
    read*, pageWidth
    line = ""
    do
       read*, word
       if (word .eq. 'xxx') exit
       ! process word
       if (len(line) + 1 + len(word) .le. pageWidth) then
          line = line // " " // word
       else
          print*, line
          line = word
       end if
    end do
    print*, line
    end
    
  2. Compile the program as you learned in Lab1.

Questions

  1. First you will have to create a text file of words, using Notepad. Copy a few sentences for a book, magazine or newspaper. The first entry in the file must a number indicating the desired page width. Place each word on a separate line. The word can be placed anywhere on the line. Leave some lines blank as well. End the file with xxx. No quotes are needed around anything. Save the file as a text file.
  2. Test this data file by writing a small Fortran program that reads one word at a time and immediately prints it out. The output will be a column of single words. The programs reads the data from the input file by file redirection
    C:\F\York>Lab5B < data.txt
  3. Modify the previous program to not only output the word, but also its length. The length is the number of characters it contains. Do this by scanning the word left to right to locate the first blank character. The length is one less than the position of the first blank in the string.
  4. Now you are ready to modify the proposed solution to correctly build the line. Instead of using the len function, you will keep track of the actual length of the line and word in integer variables.

Fall2002/HR
Fall2011/JH