%-------------------------------------% % Lab9 p.3 % % Date: 11/5/02 % % % % Gun Srijuntongsiri % % gs61 % % ?????? % %-------------------------------------% % Variables % --------- % tmpdata = cell containing the series of user-inputted strings % data = array containing the series of (padded) strings % maxlength = the length of the longest string % minidx = the index of currently "smallest" string tmpdata = {}; % Begin with no string num = 0; % same with num % Ask for first string str=input('Enter a string (empty string to quit): ','s'); % While the inputted string is not empty while ~isempty(str) num=num+1; % increase num tmpdata{num}=str; % put the string into data % Ask for next string str=input('Enter a string (empty string to quit): ','s'); end data = char(tmpdata); % Convert to normal arrays and pad all strings to % the same length. maxlength = size(data,2); % Max length of all strings for i=1:num-1 minidx = i; for j=i+1:num k=1; % Begin comparing from the first letter % If the current characters are the same, compare the next one. while k<=maxlength & data(minidx,k)==data(j,k) k=k+1; end % If the strings are not exactly equal, and the minidx-th is larger, if kdata(j,k) minidx = j; % Let the j-th string be the minimum. end end tmp = data(minidx,:); data(minidx,:) = data(i,:); data(i,:) = tmp; end disp('Below is the sorted series of strings.'); disp(data)