%-------------------------------------% % Lab9 p.2 % % Date: 11/5/02 % % % % Gun Srijuntongsiri % % gs61 % % ?????? % %-------------------------------------% % Variables % --------- % str = user-input string to search on % ch = user-input character to count % count = number of $ch$ that appears in $str$ % Ask for the string and the character str=input('Enter a string: ','s'); ch =input('Enter a character to count in the string: ','s'); % Check for bad input if (size(ch,1)~=1)|(size(ch,2)~=1) error('You need to enter exactly ONE character.'); end count = 0; % Initialize count to zero. % Count by checking each character in $str$ against $ch$ for i=1:size(str,2) if str(i)==ch % If this character is $ch$, count=count+1; % then increment count. end end % Output disp(['There are ',num2str(count),' occurrences of ', ch,'.']);