File: ef230-2023-08/www/examples/text_bottles.m Download
%! Example of working with text
%! Displays the complete lyrics to '99 bottles of beer...'

function main
clear all;
clc;

fp=1;
for ii=99:-1:1
    s1 = numword(ii,'bottle','bottles');
    s2 = numword(ii-1,'bottle','bottles');
    fprintf(fp,'%s of beer on the wall, %s of beer. Take one down, pass it around, %s of beer on the wall!\n',capit(s1),s1,s2);
end
return

function ss = capit(s)
    ss = [upper(s(1)) s(2:length(s))];
return

function ss = numword(num,su,pu)
    tens = {'ten';'twenty';'thirty';'forty';'fifty';'sixty';'seventy';'eighty';'ninety'};
    ones = {'zero';'one';'two';'three';'four';'five';'six';'seven';'eight';'nine'};
    teens = {'ten';'eleven';'twelve';'thirteen';'fourteen';'fifteen';'sixteen';'seventeen';'eighteen';'nineteen'};
    t = floor(num/10);
    o = num - t*10 + 1 ;
    if num >= 20
        ss = char(tens(t));
        if o~=1; ss=[ss '-' char(ones(o))]; end;
    elseif num >= 10
        ss = char(teens(num-9));
    else
        ss = char(ones(o));
    end
    if (num==1)
        ss = [ss ' ' su];
    else
        ss = [ss ' ' pu];
    end
return