1、创建字符串a“This is an example.”,获取字符串所对应的ASCII码数值数组为b,再把ASCII码数组b变为字符串c。找出字符串c中小写字母的元素位置赋给w,统计小写字母的个数,并把对应位置的小写字母ASCII值减32变成大写字母,得到的字符串为d。判断d中是否都为字母。再将d变成首字母大写,其他小写的字符串e。(使用通用命令,即未知字符串内容也可实现功能)。
你好,代码供参考:
a = 'This is an example.';
b = double(a) %获取字符串所对应的ASCII码数值数组为b
c = char(b)%再把ASCII码数组b变为字符串c
w = c(isstrprop(b,'lower'))%找出字符串c中小写字母的元素位置赋给w
numel(w)%统计小写字母的个数
d = c;
%并把对应位置的小写字母ASCII值减32变成大写字母,得到的字符串为d
d(isstrprop(b,'lower')) = char(double(w) - 32)
all(isstrprop(d,'alpha'))%判断d中是否都为字母
e = d;
% 再将d变成首字母大写,其他小写的字符串e
e(isstrprop(e,'upper')) = lower(e(isstrprop(e,'upper')));
e(1) = upper(e(1))
输出结果:
b =
列 1 至 15
84 104 105 115 32 105 115 32 97 110 32 101 120 97 109
列 16 至 19
112 108 101 46
c =
'This is an example.'
w =
'hisisanexample'
ans =
14
d =
'THIS IS AN EXAMPLE.'
ans =
logical
0
e =
'This is an example.'
答题不易,有帮助望采纳呢,谢谢啦