Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting letters in a given sentence

Status
Not open for further replies.

frtran90

Programmer
Feb 19, 2009
11
US
I'm trying to create a program that assigns a sentence to a given character variable, prompts the user to input a letter, and then outputs how many times that letter is in the assigned sentence. I'm trying to do this with a simple do loop..

completely stuck. could anyone show me a code that would do it?
 
Something like this?

Code:
[COLOR=#a020f0]module[/color] my_functions
 [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#a020f0]contains[/color]
  [COLOR=#2e8b57][b]integer[/b][/color] [COLOR=#a020f0]function[/color] count_chars(ch, str)
    [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
    [COLOR=#2e8b57][b]character[/b][/color] :: ch, ss
    [COLOR=#2e8b57][b]character[/b][/color] ([COLOR=#2e8b57][b]len[/b][/color][COLOR=#804040][b]=*[/b][/color]):: str
    [COLOR=#2e8b57][b]integer[/b][/color] :: j, num

    count_chars [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
    [COLOR=#804040][b]do[/b][/color] j [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], [COLOR=#008080]len_trim[/color](str)
      ss[COLOR=#804040][b]=[/b][/color]str(j:j[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color])
      [COLOR=#804040][b]if[/b][/color] (ch [COLOR=#804040][b]==[/b][/color] ss) [COLOR=#804040][b]then[/b][/color]
        count_chars [COLOR=#804040][b]=[/b][/color] count_chars [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
      [COLOR=#804040][b]endif[/b][/color]
    [COLOR=#804040][b]enddo[/b][/color]  
  [COLOR=#a020f0]end function[/color] count_chars
[COLOR=#a020f0]end module[/color] my_functions

[COLOR=#a020f0]program[/color] sentence
  [COLOR=#a020f0]use[/color] my_functions
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]parameter[/b][/color] :: n [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]100[/color]
  [COLOR=#2e8b57][b]character[/b][/color] ([COLOR=#2e8b57][b]len[/b][/color][COLOR=#804040][b]=[/b][/color]n) :: snt
  [COLOR=#2e8b57][b]character[/b][/color] :: c

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Enter sentence: '[/color]
  [COLOR=#804040][b]read[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]10[/color]) snt
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Enter character: '[/color]
  [COLOR=#804040][b]read[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]10[/color]) c
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Character occurence = '[/color], count_chars(c, snt)
  
  [COLOR=#6a5acd]10[/color] [COLOR=#804040][b]format[/b][/color](A) [COLOR=#0000ff]! character format[/color]
[COLOR=#a020f0]end program[/color] sentence

Output:
Code:
$ g95 sentence.f95 -o sentence

$ sentence
 Enter sentence: 
Example sentence
 Enter character: 
e
 Character occurence =  4
 
program countletter
implicit none
integer::i,j
integer::numberoftimes
character::letter
character::letter1
character(len=30)::sentence = "I don't know how to do it."
write(*,*)'Enter a letter'
read(*,*)letter
write(*,*)"You chose the letter '",letter, "' and the number of times it appear$

loop1: do i=1, len_trim(sentence)
letter1=sentence(i:i)
if(letter==letter1)then
write(*,*)'I found your letter'
!loop2: do j=1,i
!end do loop2
end if
end do loop1

write(*,*)'The sentence is "',sentence,'"'
write(*,*)len_trim(sentence)
end program



thats what i have so far. the output is just "i found your letter" as many times as the letter appears in the sentence.. i need to have it say something like 'the letter you chose appears',numberoftimes,'times.
 
hi mikrom,

that program works, but i'm not sure i understand it exactly.
is there any way you could add something to my code to make it print the number of times the letter appears?
 
Oops, in my code above I have
ss=str(j:j+1)
and this is not correct, I have to use
ss=str(j:j)
like you
:)

Here I added to your code what you need
Code:
[COLOR=#a020f0]program[/color] countletter
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: i, numberoftimes
  [COLOR=#2e8b57][b]character[/b][/color] :: letter, letter1
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#2e8b57][b]len[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]30[/color])::sentence [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"I don't know how to do it."[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])[COLOR=#ff00ff]'Enter a letter'[/color]
  [COLOR=#804040][b]read[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) letter

  numberoftimes [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
  [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#008080]len_trim[/color](sentence)
    letter1[COLOR=#804040][b]=[/b][/color]sentence(i:i)
    [COLOR=#804040][b]if[/b][/color] (letter[COLOR=#804040][b]==[/b][/color]letter1) [COLOR=#804040][b]then[/b][/color]
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'I found your letter'[/color]
      numberoftimes [COLOR=#804040][b]=[/b][/color] numberoftimes [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
    [COLOR=#804040][b]end if[/b][/color]
  [COLOR=#804040][b]end do[/b][/color]

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"The sentence is '"[/color], [COLOR=#008080]trim[/color](sentence), [COLOR=#ff00ff]"'"[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"You chose the letter '"[/color], letter, [highlight #ffff00][COLOR=#0000ff]&[/color][/highlight]
    [COLOR=#ff00ff]"' and the number of times it appears is "[/color], numberoftimes
[COLOR=#a020f0]end program[/color] countletter

Output:
Code:
$ g95 sentence_02.f95 -o sentence_02

$ sentence_02
 Enter a letter
n
 I found your letter
 I found your letter
 The sentence is 'I don't know how to do it.'
 You chose the letter 'n' and the number of times it appears is  2

$ sentence_02
 Enter a letter
o
 I found your letter
 I found your letter
 I found your letter
 I found your letter
 I found your letter
 The sentence is 'I don't know how to do it.'
 You chose the letter 'o' and the number of times it appears is  5
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top