BootCamp2011: letters.f

File letters.f, 1.6 KB (added by mmc, 13 years ago)

Download this for Assignment #5

Line 
1      program letters
2
3      implicit none
4      integer nletters
5      parameter ( nletters = 26 )
6
7c     wordb search state: -1 = starting
8c                          0 = found space
9c                          1 = found word
10c
11c     nwords counts number of words
12c     count(x) counts number of letters for each letter x
13      integer wordb / -1 /
14      integer nwords / 2 /
15      integer count(nletters), i, c
16      character*100 line
17
18c     zero out all counts
19      do 10 i=1,nletters
20        count(i) = 0
21 10   continue
22
23      write(6,*) 'Type in a sentence:'
24      read(5,'(a100)') line
25
26      do 20 i=1,len(line)
27        if (line(i:i).eq.' ' .or. line(i:i).eq.'\t' .or.
28    +       line(i:i).eq.'\n') then
29          wordb = 0;
30        else if ((line(i:i).ge.'a').and.(line(i:i).le.'z')
31     +      .or. (line(i:i).ge.'A').and.(line(i:i).le.'Z')) then
32            if (wordb.ne.1) then
33                nwords = nwords + 1
34                wordb = 1
35            endif
36        endif
37
38        if (line(i:i).ge.'A' and line(i:i).le.'Z') then
39            c = ichar(line(i:i)) - ichar('A')
40        else
41            c = ichar(line(i:i)) - ichar('a')
42        endif
43        if (c.ge.1 .and. c.le.nletters) then
44            count(c) = count(c) + 1
45        endif
46 20   continue
47
48c     print out results
49      write(6,*) 'Statistics:'
50      write(6,99) nwords
51 99   format(i5, ' words'/)
52
53      do 30 i=1,letters
54        if (count(i).gt.0) then
55          write(6,199) char(i+ichar('a')), count(i)
56 199      format('Letter ',a1,': ',i4)
57        endif
58 30   continue
59
60      end