Notes on Characters and Strings - Introduction to Computing Using MATLAB | CS 1112, Study notes of Computer Science

Material Type: Notes; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Fall 2008;

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-8hb-1
koofers-user-8hb-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Lecture 17 Oct 28, 2008
Lecture slides 1
Previous Lecture:
Working with images
Today’s Lecture:
Characters and strings
Very brief introduction to recursion—more later
Announcements:
Section will be in the classrooms this week
Project 4 posted. Due Thurs10/30 at 6pm.
October 28, 2008 Lecture 17 4
Characters & strings
We have already used strings:
n= input(‘Next number:’)
fprintf(‘Answer is %d’, ans)
A string is made up of individual characters, so a string is
a 1-d array of characters
The character array ‘CS1112 rocks! is of length 13,
has 7 letters, 4 digits, 1 space, and 1 symbol.
Use single quotes to enclose characters:
‘100’ is a character array of length 3
100 is a numeric value
October 28, 2008 Lecture 17 7
Strings as vectors
Vectors
Assignment
v= [7 0 5];
Indexing
x= v(3); % x is 5
v(1)= 1; % v is [1 0 5]
w= v(2:3); % w is [0 5]
:notation
v= 2:5; % v is [2 3 4 5]
Appending
v= [7 0 5];
v(4)= 2; % v is [7 0 5 2]
Concatenation
v= [v [4 6]];
% v is [7 0 5 2 4 6]
Strings
Assignment
s= ‘hello’;
Indexing
c= s(2); % c is ‘e’
s(1)= ‘J’; % s is ‘Jello’
t= s(2:4); % t is ‘ell’
:notation
s= ‘a’:‘g’; % s is ‘abcdefg’
Appending
s= ‘duck’;
s(5)= ‘s’; % s is ‘ducks’
Concatenation
s= [s ‘ quack’];
% s is ‘ducks quack’
October 28, 2008 Lecture 17 8
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 1st letter
Write a function to capitalize the first letter of each word
in a string. Assume that the string has lower case letters
and 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 spaces
Look For The Spaces
October 28, 2008 Lecture 17 11
Character vs ASCII code
str= ‘Age 19’
%a 1-d array of characters
code= double(str)
%convert chars to ascii values
str1= char(code)
%convert ascii values to chars
pf2

Partial preview of the text

Download Notes on Characters and Strings - Introduction to Computing Using MATLAB | CS 1112 and more Study notes Computer Science in PDF only on Docsity!

CS1112 Lecture 17 Oct 28, 2008

Lecture slides 1

„ Previous Lecture: „ Working with images

„ Today’s Lecture: „ Characters and strings „ Very brief introduction to recursion—more later

„ Announcements: „ Section will be in the classrooms this week „ Project 4 posted. Due Thurs10/30 at 6pm.

October 28, 2008 Lecture 17 4

Characters & strings „ We have already used strings: „ n= input(‘Next number:’) „ fprintf(‘Answer is %d’, ans) „ A string is made up of individual characters, so a string is a 1-d array of characters „ The character array ‘CS1112 rocks!’ is of length 13, has 7 letters, 4 digits, 1 space, and 1 symbol. „ Use single quotes to enclose characters: „ ‘100’ is a character array of length 3 „ 100 is a numeric value

October 28, 2008 Lecture 17 7

Strings as vectors Vectors „ Assignment v= [7 0 5]; „ Indexing x= v(3); % x is 5 v(1)= 1; % v is [1 0 5] w= v(2:3); % w is [0 5] „ : notation v= 2:5; % v is [2 3 4 5] „ Appending v= [7 0 5]; v(4)= 2; % v is [7 0 5 2] „ Concatenation v= [v [4 6]]; % v is [7 0 5 2 4 6]

Strings „ Assignment s= ‘hello’; „ Indexing c= s(2); % c is ‘e’ s(1)= ‘J’; % s is ‘Jello’ t= s(2:4); % t is ‘ell’ „ : notation s= ‘a’:‘g’; % s is ‘abcdefg’ „ Appending s= ‘duck’; s(5)= ‘s’; % s is ‘ducks’ „ Concatenation s= [s ‘ quack’]; % s is ‘ducks quack’ October 28, 2008 Lecture 17 8

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 1st^ letter Write a function to capitalize the first letter of each word in a string. Assume that the string has lower case letters and 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 spaces Look For The Spaces (^) October 28, 2008 Lecture 17 11

Character vs ASCII code

str= ‘Age 19’ %a 1-d array of characters code= double(str) %convert chars to ascii values str1= char(code) %convert ascii values to chars

CS1112 Lecture 17 Oct 28, 2008

Lecture slides 2

October 28, 2008 Lecture 17 12

Arithmetic and relational ops on characters „ ‘c’-‘a’ gives 2 „ ‘6’-‘5’ gives 1 „ letter1=‘e’; letter2=‘f’; „ letter1-letter2 gives -

„ ‘c’>’a’ gives true „ letter1==letter2 gives false

„ ‘A’ + 2 gives 67 „ char(‘A’+2) gives ‘C’ October 28, 2008 Lecture 17 13

What is in variable g (if it gets created)? d1= ‘Oct 3’; d2= ‘Oct 9’; x1= d1(5); x2= d2(5); g= x2-x1; A: the character ‘ 6 ’ B: the number 6 C: Error in the subtraction operation D: Error in assigning variables x1, x E: Some other value or error

October 28, 2008 Lecture 17 14

What is in variable g (if it gets created)? d1= ‘Oct 13’; d2= ‘Oct 29’; x1= d1(5:6); x2= d2(5:6); g= x2-x1; A: the string ‘ 16 ’ B: the number 16 C: Error in the subtraction operation D: Error in assigning variables x1, x E: Some other value or error October 28, 2008 Lecture 17 15

Example: toUpper Write a function toUpper(cha) to convert character cha to upper case if cha is a lower case letter. Return the converted letter. If cha is not a lower case letter, simply return the character cha. Hint: Think about the distance between a letter and the base 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’

October 28, 2008 Lecture 17 16

Example: removing all occurrences of a character „ From a genome bank we get a sequence ATTG CCG TA GCTA CGTACGC AACTGG AAATGGC CGTAT… „ First step is to “clean it up” by removing all the blanks. Write this function:

function s = removeChar(c, s) % Return string s with all occurrences % of character c removed

October 28, 2008 Lecture 17 17

Example: removing all occurrences of a character „ Can solve this problem using iteration—check one character (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 s Same 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