%-------------------------------------% % Homework 7: p.2 % % Date: 11/11/2002 % % % % Gun Srijuntongsiri % % gs61 % % ?????? % %-------------------------------------% % % MYSTRCMP Compare two strings and return 1 if identical, 0 otherwise. % Note: To test this function, save this file as MYSTRCMP.M function result=mystrcmp(s1,s2) % Parameters: % s1, s2 = two strings to be compared % % Variables: % result = answer of the function % len1 = Length of s1 % len2 = Length of s2 % Find the length of the two strings len1 = length(s1); len2 = length(s2); % If they are not of equal length, then they are not identical if len1 ~= len2 result = 0; return end i = 1; % Otherwise, compare each pair of characters one at a time, % until find a pair that is different or finish all pairs. while (i<=len1) & (s1(i)==s2(i)) i=i+1; end if i <= len1 % If found a non-identical pair, result = 0; % then return negative result. else % Otherwise, result = 1; % return positive result. end