Lab 19
Strings and String Functions
CS211 Lab Policy:
- This lab exercise will not be graded.
- Submit as much as you have completed before the end of the lab period in
which it is assigned.
- If you do not finish this lab work, it is to your advantage to finish it
outside of class. Please re-submit your finished work to the course web
site.
- You may receive help from anyone in completing this lab.
- You may not submit another student's code as part of your
lab.
Instructions:
Create one program named Lab19
that includes code to solve each of the following small problems. Save your
program in a file named Lab19.m. .
- Write code that gets two strings from the user and then tells the user
whether the strings are: 1) identical including the case of all letters, 2)
the same when letter case differences are ignored, or 3) different even when
case differences are ignored.
- It is common to use the string processing functions to manipulate file
names.
Add code to Lab19() that gets a file name from
the user. If the entered file name does not contain any extension (it does
not include a period), append the string '.m'
to the end of the file name. Then use the
exist() function to check if the file exists in MATLAB's search
path (check for a return value of 2). If the file is found, display a
message that tells the user that the file exists in the search path. Otherwise,
display a message saying the file does not exist in the search path. In
both cases, include the full file name in the message displayed to the user.
- A palindrome is a word or phrase that is spelled the same
forwards or backwards (like "a", "bb", and "ma'am").
- Write a sub-function named Is_palindrome()
with one string input argument and one logical output argument. The
function should return true if the input
argument is a palindrome (ignoring case) and
false otherwise. Place this function after the end of function
Lab19(). (Hint: look up the
fliplr() MATLAB function using
the help system.)
- Add code to Lab19() that gets
a string from the user, calls your Is_palindrome() function and then reports whether or not the
user-entered string is a palindrome (ignoring case).
- Many clever long palindromes include spaces and punctuation marks which
are ignored for the purposes of counting the text as a palindrome. For
example, "I prefer pi." and "A man, a plan, a canal - Panama!" are
considered palindromes.
- Write a subfunction named Just_letters
with one input argument and one output argument. The function should
return the input string with all characters, except letters, removed.
For example, when called with the input argument string:
'a man, a plan, a canal - panama!'
the function Just_letters should return
the string:
'amanaplanacanalpanama'
(Hint: You can write this operation with a single assignment statement
using the
isstrprop()
function in combination with the
find()
function.)
- Add code to function Lab19() that is
identical to your code for Part 3 above, but call
Just_letters before calling
Is_palindrome, so that spaces and
punctuation marks are not considered when reporting whether or not the
user input string is a palindrome.
Turn-in:
Submit your
Lab19.m file.