Counting number of occurances of a letter in a sentence

Discussion in 'Assembly Language Programming (ALP) Forum' started by stuckInRainbows, Nov 2, 2008.

  1. stuckInRainbows

    stuckInRainbows New Member

    Joined:
    Nov 2, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I'm working on an 8085 program to read sentences typed in by a user and then count the number of occurrences of a specific letter requested by the user.

    Program begins with a prompt for a sentence- user can end the sentence by either hitting the return key or typing a period. The program should then ask for a letter. It should then count the number of occurrences of that letter in the sentence and print it to the
    screen. User may type either a small or a capital letter. In either of the two cases, occurrences of both the small and the capital letter should be printed.

    How does one approach this?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well, you've got the outline for the program already. You need in general two things for a computer program: (1) some storage; (2) some processing. What kind of each do you think you'll need? Have you done a dry run on paper to see how in detail you might do it?
     
  3. stuckInRainbows

    stuckInRainbows New Member

    Joined:
    Nov 2, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Well I want to count the number of letterers in the sentence and then compare each against the input, and eventually output the result.

    -define the location of the start of the sentence
    -define the location for the count of characters in the sentence.
    -define the location to put the letter to be searched for.
    -define the location of the result

    So I need to input the letter to be searched for, or load the memory location of the first letter of the sentence in a register

    Then I guess I should create a loop to count the number of letters, stop the count at the period or carriage return then save the result.

    Afterward I need a loop to compare the letter i'm looking for (upper and lowercase) against a letter in the sentence, while simultaneously incrementing two counters.

    And i'll also have to take into account what to do if a condition is not met.

    I'm just fuzzy on writing the actual code to accomplish these, especially the loops.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Seems a little overcomplicated, you only need one loop that terminates when you get to the end of the sentence, and during the loop you can count the occurrences of the letter and the number of letters in the sentence, if you need that.

    The loop variable itself could either be a pointer to the current memory location being considered, or it could be an offset. In C this could be written as
    Code:
    int i=0;
    for(;;)
    {
      // look at str[i], which is *(str+i)
    }
    
    or
    Code:
    char *i=str;
    for(;;)
    {
      // look at *str
    }
    
    Do you have an example of a loop in the course notes you've got so far? Can you adapt that loop to do what you want?

    If you can't see how, then post what you've got (8085 code isn't one I've met) and we can suggest what you might change to get what you want.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice