0% found this document useful (0 votes)
9 views20 pages

Chapter 4 - Part 3 - Matlab - Strings

The document provides an introduction to strings in computing, explaining their representation as arrays of characters and various operations that can be performed on them. It covers string manipulation functions such as conversion between strings and numerical arrays, comparison of strings, and string replacement. Additionally, it includes examples and practice problems for implementing string operations in MATLAB.

Uploaded by

vpnqynh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

Chapter 4 - Part 3 - Matlab - Strings

The document provides an introduction to strings in computing, explaining their representation as arrays of characters and various operations that can be performed on them. It covers string manipulation functions such as conversion between strings and numerical arrays, comparison of strings, and string replacement. Additionally, it includes examples and practice problems for implementing string operations in MATLAB.

Uploaded by

vpnqynh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Introduction to Computing

Huynh Vo Trung Dung, Ph.D.


 A string is an array of characters
s = 'abc‘ is equivalent to s = [ 'a' 'b' 'c' ]
 All operations that apply to vectors and arrays
can be used together with strings as well
◦s(1) → 'a'
◦s( [ 1 2 ] ) = 'XX' → s = 'XXc'
◦s(end) → 'c'
Letter Digit Letter Digit Letter Digit Letter Digit

a 97 n 110 A 65 N 78
b 98 o 111 B 66 O 79
c 99 p 112 C 67 P 80
d 100 q 113 D 68 Q 81
e 101 r 114 E 69 R 82
f 102 s 115 F 70 S 83
g 103 t 116 G 71 T 84
h 104 u 117 H 72 U 85
i 105 v 118 I 73 V 86
j 106 w 119 J 74 W 87
k 107 x 120 K 75 X 88
l 108 y 121 L 76 Y 89
m 109 z 122 M 77 Z 90
Symbol Digit Symbol Digit Symbol Digit Symbol Digit

32 - 45 : 58 { 123
! 33 X. 46 ; 59 | 124
“ 34 / 47 < 60 } 125
# 35 0 48 = 61
$ 36 1 49 > 62
% 37 2 50 ? 63
& 38 3 51 @ 64
‘ 39 4 52 [ 91
( 40 5 53 \ 92
) 41 6 54 ] 93
* 42 7 55 ^ 94
+ 43 8 56 _ 95
, 44 9 57 ` 96
 Conversion of strings to numerical arrays
◦ double( 'abc xyz' )
ans =
97 98 99 32 120 121 122
◦ double( 'ABC XYZ' )
ans =
65 66 67 32 88 89 90
 Conversion of numerical arrays to strings
◦ char( [ 72 101 108 108 111 33 ] )
ans =
Hello!
 ischar() : returns 1 for a character array
◦ ischar ( 'CS 111' )
ans =
1
 isletter() : returns 1 for letters of the alphabet
◦ isletter( 'CS 111' )
ans =
1 1 0 0 0 0
 isspace() : returns 1 for whitespace (blank, tab, new
line)
◦ isspace( 'CS 111' )
ans =
0 0 1 0 0 0
 Comparing two characters
◦ 'a' < 'e'
ans =
1
 Comparing two strings character by character
◦ 'fate' == 'cake'
ans =
0 1 0 1
◦ 'fate' > 'cake'
ans =
1 0 1 0
 strcmp() : returns 1 if two strings are identical
◦ a = 'Bilkent';
◦ strcmp( a, 'Bilkent' )
ans =
1
◦ strcmp( 'Hello', 'hello' )
ans =
0
 strcmpi() : returns 1 if two strings are identical
ignoring case
◦ strcmpi( 'Hello', 'hello' )
ans =
1
 findstr() : finds one string within another one
◦ test = 'This is a test!';
◦ pos = findstr( test, 'is' )
pos =
3 6
◦ pos = findstr( test, ' ' )
pos =
5 8 10
 strrep() : replaces one string with another
◦ s1 = 'This is a good example';
◦ s2 = strrep( s1, 'good', 'great' )
s2 =
This is a great example
 Recall num2str() for numeric-to-string
conversion
◦ str = [ 'Plot for x = ' num2str( 10.3 ) ]
str =
Plot for x = 10.3

 str2num() : converts strings containing numbers


to numeric form
◦ x = str2num( '3.1415' )
x=
3.1415
 The fprintf( format, data ) function
◦ %d integer
◦ %f floating point format
◦ %e exponential format
◦ %n new line character
◦ %t tab character
◦ fprintf( 'Result is %d', 3 );
Result is 3
◦ fprintf( 'Area of a circle with radius %d is %f', 3, pi*3^2
);
Area of a circle with radius 3 is 28.274334
◦ x = 5;
◦ fprintf( 'x = %d', x );
x= 5
◦ x = pi;
◦ fprintf( 'x = %0.2f', x )
x = 3.14
◦ fprintf( 'x = %0.5f%n', x )
x = 3.14159
◦ fprintf( 'x = %d%ny = %d%n', 3, 13 );
x=3
y = 13
Example

Compare two strings according to the order of their


characters in the ASCII table.
>> string1='abcdef'

string1 =

abcdef

>> string2='abcdef'

string2 =

abcdef

>> check_strcmp(string1, string2)

ans =

0
Solution

1. State the problem:


Write a function that will compare two strings string1 and
string2, and return the following result:
-1 if string1 is less than string2
0 if string1 is equal to string2
1 string1 is greater than string2
2. Define the inputs and outputs
Inputs: 2
Output: 1
Solution

3. Describe the algorithm


Verify the input strings
Compare characters from the beginning to end, looking for
the first difference.
Return a value based on the first difference.
Solution

4. Turn the algorithm into Matlab code

function result=check_strcmp(str1,str2) %define the function, number of inputs and outputs


if ~(ischar(str1)&ischar(str2)) %check the strings
error('Both str1 and str2 must both be strings')
else
strings=strvcat(str1,str2); % Concatenate strings vertically, as a matrix
diff=strings(1,:)~=strings(2,:); %the difference of 2 strings, array
if sum(diff)==0
result=0;
else
ival=find(diff); %list the indices of nonzero elements, array
if strings(1,ival(1))>strings(2,ival(1))%find the first difference between strings
result =1;
else
result =-1;
end
end
end
PRACTICE

1. Write a program that accepts an input string from the user


and determines how many times a user-specified character
appears within the string.

Enter the string: 'abccfedc'


The character needs to be checked: 'c'
The times of appearance of checked character is: 3
PRACTICE

2. Write a program that accepts a series of characters from


the user, sort the characters into ascending order, and prints
them out.
PRACTICE

3. Write a program that accepts a series of characters from


the user, check the symmetricity of that string.

You might also like