用input函数输入一个同时包含大小写字母、数字和特殊符号(如空格、“*”、“@”等)的字符串,通过2个脚本分别实现字符串内字符的大写到小写以及小写到大写转换(注:大小写转换只针对26个字母,字符串中除26个字母外的其他字符,如数字、特殊符号等,应保持原格式输出)。
利用大小写之间相差 'a'-'A'这么多,也就是ASCII码相减 97-65=32
Big2Small.m
function [ strOut ] = Big2Small( strIn )
mCount = length(strIn);
strOut = strIn;
for i=1:1:mCount
temp = strIn(i);
if(temp>='A' &&temp<='Z')
a=0;
strOut(i) = strOut(i)+('a'-'A');
end
end
end
Small2Big.m
function [ strOut ] = Small2Big( strIn )
mCount = length(strIn);
strOut = strIn;
for i=1:1:mCount
temp = strIn(i);
if(temp>='a' && temp<='z')
a=0;
strOut(i) = strOut(i)-('a'-'A');
end
end
end
验证的.m
str = input('输入。。。。','s');
resultstr =Big2Small(str);
%resultstr =Small2Big(str);
disp([str,' 被转化成了:',resultstr]);