a)
Code Block | ||
---|---|---|
| ||
function boolean = isPalindrome(word)
word = lower(word);
for i = 1:floor(length(word)/2)
if word(i) ~= word(length(word)-i+1)
boolean = false;
return
end
end
boolean = true;
end |
b)
Code Block | ||
---|---|---|
| ||
function boolean = isPalindromeSentence(sentence) word = ''; counter = 1; for i = 1:length(sentence) if strcmp(sentence(i), ' ') || strcmp(sentence(i), '.') || strcmp~isletter(sentence(i), ',') continue end word(counter) = sentence(i); counter = counter + 1; end boolean = isPalindrome(word); end |
c)
Code Block | ||
---|---|---|
| ||
function resindex = sequenceFinder containsSubstring(sequencefirst,start,stop second) len = 0; index = 0-1; for i = 1:length(sequence)-length(stop) if (length(start) <= length(sequence(i:end))) && strcmp(start,sequence(i:i+length(start)-1)) : length(first) - length(second) + 1 indexS = i; elseif (length(stop) <= length(sequence(i:end))) && strcmp(stop,sequence(i:i+length(start)-1)if strcmp(first(i : i + length(second) - 1), second) indexEindex = i; if len < indexE -indexS len = indexE -indexS;return index = indexS; end end end res = sequence(index:index+len+length(stop)-1); end |
function res = sequenceFinder (sequence,start,stop )
len =
0
;
index =
0
;
for
i =
1
:length(sequence)-length(stop)
if
(length(start) <= length(sequence(i:end))) && strcmp(start,sequence(i:i+length(start)-
1
))
indexS = i;
elseif (length(stop) <= length(sequence(i:end))) && strcmp(stop,sequence(i:i+length(start)-
1
))
indexE = i;
if
len < indexE -indexS
len = indexE -indexS;
index = indexS;
end
end
end
res = sequence(index:index+len+length(stop)-
1
);
end |
Eventuelt kan den lages litt enklere ved hjelp av matlabs strfind() funksjon.