Docsity

Characters and Strings, Introduction to Recursion | CS 1112, Study notes of Computer Science

Partial preview of the text

Download Characters and Strings, Introduction to Recursion | CS 1112 and more Study notes Computer Science in PDF only on Docsity!

„^ Previous Lecture:^ „^ Working with images „^ Today’s Lecture:^ „^ Characters and strings^ „^ Brief introduction to recursion—more later „^ Announcements:^ „^ Section will be in the classrooms this week^ „^ Project 4 posted. Due Tues 3/31 at 11pm.

March 24, 2009^

Lecture 17^

Characters & strings „^ We have used strings already:^ „^ n= input(‘Next number: ’)^ „^ sprintf(‘Answer is %d’, ans) „^ A string is made up of individual characters, so astring is a 1-d array of characters „^ ‘ CS

rocks!

’ is a character array of length

13; it has 7 letters, 4 digits, 1 space, and 1symbol.

C^ S^ 1 11 2

r^ o^ c^ k^ s^!

March 24, 2009^

Lecture 17^

Numerical data is often encoded in strings. E.g., a filecontaining Ithaca weather data begins with the string

W07629N

meaning

Longitude:

o^76 29’ West Latitude:

o^ 26’ North

We may need to grab hold of the substring

W

convert

^076 and

^29 to the numeric values 76 and 29, and

Strings are important in computationdo some computation

March 24, 2009^

Lecture 17^

Comparison of genomic sequences is another example ofstring computation „^ E.g., looking for a pattern:Given the sequence

ATTCTGACCTCGATC…

Look for the pattern

ACCT

„^ E.g., quantifying the difference betweensequences:

ATTCTGACCTCGATCATTCGTGACCTCGAT

What if this nucleotide is removed?

March 24, 2009^

Lecture 17^

Some useful string functions str= ‘Cs 1112’;length(str)

% 7 isletter(str)

% [1 1 0 0 0 0 0] isspace(str)

% [0 0 1 0 0 0 0] lower(str)

% ‘cs 1112’ upper(str)

% ‘CS 1112’ ischar(str)% Is str a char array? True (1)strcmp(str(1:2),‘cs’)% Compare strings str(1:2) & ‘cs’. False (0)strcmp(str(1:3),‘CS’)% False (0)

Example: capitalize 1

st^ letter

Write a function to capitalize the first letter of each wordin a string. Assume that the string has lower case lettersand blanks only.function [str, nCaps] = caps(str)% Post: Capitalize first letter of each word.%^ str = partially capitalized string%^ nCaps = no. of capital letters% Pre: str = string with lower case letters & blanks only

look for the spacesLook For The Spaces

March 24, 2009^

Lecture 17^

Character vs ASCII code str=

‘Age

%a^ 1-d

array

of^

characters

code=

double(str)%convert

chars

to^

ascii

values

str1=

char(code)%convert

ascii

values

to^

chars

March 24, 2009^

Lecture 17^

Arithmetic and relational ops on characters „^ ‘c’-‘a’

gives 2

„^ ‘6’-‘5’

gives 1

„^ letter1=‘e’;

letter2=‘f’;

„^ letter1-letter

gives -

„^ ‘c’>’a’

gives true

„^ letter1==letter

gives false

„^ ‘A’

+^2

gives 67

„^ char(‘A’+2)

gives ‘C’

March 24, 2009^

Lecture 17^

What is in variable g (if it gets created)? d1=^

‘Mar

d2=

‘Mar

x1=^

d1(5:6);

x2=

d2(5:6);

g=^ x2-x1;^ A: the string ‘

B: the number 16C: Error in the subtraction operationD: Error in assigning variables

x1 ,^ x

E: Some other value or error

March 24, 2009^

Lecture 17^

Example: toUpperWrite a function toUpper(cha) to convert character cha toupper case if cha is a lower case letter. Return theconverted letter. If cha is not a lower case letter, simplyreturn the character cha.Hint: Think about the distance between a letter and thebase letter ‘a’ (or ‘A’). E.g.,

a b c d e f g h …A B C D E F G H … Of course, do not use Matlab’s function upper!

distance

= ‘g’-‘a’ = 6 = ‘G’-‘A’

March 24, 2009^

Lecture 17^

Example: removing all occurrences of a character „^ Can solve this problem using iteration—check onecharacter (one component of the vector) at a time „^ New strategy:

recursion „^ Possible when result can be accumulated iteratively „^ E.g., remove all the blanks in string sSame as remove blank in s(1)

and remove blanks in s(2:length(s)) „^ E.g., capitalize first letter of all words in a sentence „^ Same as capitalize 1

st^ letter of first word and capitalize 1

st^ letter of the rest of the words

March 24, 2009^

Lecture 17^

function s = removeChar(c, s)% Return string s with character c removedif length(s)==

% Base case: nothing to do returnelse end

March 24, 2009^

Lecture 17^

function s = removeChar(c, s)% Return string s with character c removedif length(s)==

% Base case: nothing to do returnelseif s(1)~=c% return string is% s(1) and remaining s with char c removedelse endend

March 24, 2009^

Lecture 17^

function s = removeChar(c, s)% Return string s with character c removedif length(s)==

% Base case: nothing to do returnelseif s(1)~=c% return string is% s(1) and remaining s with char c removedelse% return string is just% the remaining s with char c removedendend