Thread: Words

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Words


    Hi, guys I'm trying to calculate number of words inside the string however I'm not sure how to structure and loop and how to write it to detect the empty spaces.
    I'm using Mars Assembly here is the structure I would like to use:
    Code:
         
            .data
    text_input: .asciiz "Two words"
    word_count: .word 0
            .text
            .globl main
    main:
            la $a0, text_input    # Load address of string.
            addi $t1, $a0, 0         
            nop 
    Loop:
       lbu $t0, 0($t1) #go through string one by one
    I'm at this stage however not sure what to do next...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    928
    Where exactly are you stuck? Do you understand how to find word boundaries in the abstract (i.e. could you draw a flow chart or state machine of what you're trying to implement)? Do you understand how to write a loop over the characters in a string? Do you understand how to test the value of a character?
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0
    I'm not sure how to write a loop and detect that the is chacater and space next to each other...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    928
    You could keep track of the previous character from the string (or look at the characters stored at $t1 and $t1+1) then determine if you have a space/not-space pair.

    Or since you don't actually care what the last characters are, just store if you are currently inside of a word. Then update your state according to the next character (in-word & space -> not-in-word; in-word & not-space -> in-word; not-in-word & space -> not-in-word; not-in-word & no-space -> in-word).

    (FYI, It's easier for us to help (and we can provide better responses) if you are as detailed as possible about what you're trying to do and what is giving you difficulty. Also a good tool for solving programming problems is to write down exactly what you're trying to accomplish breaking it down into as small steps as possible. It has been a while so we may not recall where the pitfalls are until we understand what is giving you trouble.)
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

IMN logo majestic logo threadwatch logo seochat tools logo