我想知道如何检查输入字符串是否包含大写和小写组合。之后打印一条语句以显示输入字符串包含大写和小写的组合。
第0步:你需要的变量
char* str;
int i;
char found_lower, found_upper;
第一步:遍历字符串
for (int i = 0; str[i] != '\0'; i++)
第二步:检测大小写字符
found_lower = found_lower || (str[i] >= 'a' && str[i] <= 'z')
found_upper = found_upper || (str[i] >= 'A' && str[i] <= 'Z')
第 3 步:合并结果
mixed_case = found_lower && found_upper
第 4 步(可选)突破for
尽早以节省一些时间
if (found_lower && found_upper) break;
完整源代码(警告:未经测试):
char is_mixed(char* str) {
int i;
char found_lower = false, found_upper = false;
for (int i = 0; str[i] != '\0'; i++) {
found_lower = found_lower || (str[i] >= 'a' && str[i] <= 'z');
found_upper = found_upper || (str[i] >= 'A' && str[i] <= 'Z');
if (found_lower && found_upper) break;
}
return (found_lower && found_upper);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)