CMPS-2240 Lab 8
Removal of Whitespace

Gordon Griesel
Department of Computer and Electrical Engineering and Computer Science
California State University, Bakersfield

Introduction

Goals:
Input and modify a character string.
Remove the trailing whitespace from the string.
This is a right-trim function.

Resources:
Please refer to all the resources on our home page.

Files Needed:
chop.s
rev.s
read.s

Files are on Odin at: /home/fac/gordon/p/2240/code/lab8/* .


Look here at the C++ isspace function,
to see a list of characters that are considered whitespace.

Our whitespace characters will most likely be: space, tab, newline.

Lab details:

This is not a difficult lab.

Your task is to write a program trim8.s that trims the whitespace off a word entered from the keyboard. Start by copying the example files into your directory.

chop.s shortens a string by inserting a '\0' (NULL character) into the string. This effectively chops the string off. rev.s reverses the characters in a string. The algorithm takes two pointers, one starting at the end of the string and one starting at the begining of the string. It swaps the two characters at the top and bottom location. It moves the pointers 1 character (a byte) towards each other. It swaps the two characters at that location. It moves the pointers toward each other, swaps the two characters, so on and so forth until the pointers hit each other.

Your job is to write a program trim8.s that removes trailing whitespace. Everything you need to do this is in rev.s and chop.s.

Use chunks of these two sample programs. You can grab the code to prompt the user for a string from read.s.

To demonstrate that you trimmed the white space...

1. Search the string and stop at the first newline character you find.
2. Replace the newline character with a null terminator.
3. Display the string.

4. Search the string and stop at the first whitespace character you find.
5. Replace the whitespace character with a null terminator.
6. Display the string.

Don't forget to reset your pointer when searching your string again.

Note: Your program displays the period. It is not input by the user.

The code should execute as follows:



$ spim -f trim8.s

Enter a word followed by some whitespace: foo[Tab]      [Tab][Enter]
foo            .
foo.


First display the string with the whitespace, then without.
The period is displayed separately.

The [Tab] and [Enter] are not displayed!!!
They are just showing where you might press those keys.

Branch delay slot
Please add code to handle any branch delay slots.
This feature may have a great effect on your functionality, if you run spim using the -db flag.
Try it.

spim -db -f trim8.s

Your lab might be tested with the -db flag.

Extra credit...

A better right-trim function would actually start at the end of a
string, then search backwards until the first non-whitespace character
is found. A null terminator would then be placed just after the character.

You may do your second search operation this way for some extra credit.

Make note of this in comments near the top of your program.


What to turn in...

Your lab program 2240/8/trim8.s