Skip to end of metadata
Go to start of metadata

a)

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)

function boolean = isPalindromeSentence(sentence)
word = '';
counter = 1;
for i = 1:length(sentence)
    if ~isletter(sentence(i))
        continue 
    end
    word(counter) = sentence(i);
    counter = counter + 1;
end
boolean = isPalindrome(word);
end

c)

 

function index = containsSubstring(first, second)
index = -1;
for i = 1 : length(first) - length(second) + 1
    if strcmp(first(i : i + length(second) - 1), second)
        index = i; 
        return
    end
end
end

 

Eventuelt kan den lages litt enklere ved hjelp av matlabs strfind() funksjon.

  • No labels