可以使用C语言的标准库函数ctype.h
中的toupper()
和tolower()
来实现字符数组的大小写转换。
toupper()
函数将一个字符转换为大写形式,如果字符不是小写字母,则返回原字符。
tolower()
函数将一个字符转换为小写形式,如果字符不是大写字母,则返回原字符。
以下是一个使用toupper()
和tolower()
函数进行大小写转换的示例代码:
#include <stdio.h>
#include <ctype.h>
void toUpperCase(char str[]) {
int i = 0;
while (str[i] != '#include <stdio.h>
#include <ctype.h>
void toUpperCase(char str[]) {
int i = 0;
while (str[i] != '\0') {
str[i] = toupper(str[i]);
i++;
}
}
void toLowerCase(char str[]) {
int i = 0;
while (str[i] != '\0') {
str[i] = tolower(str[i]);
i++;
}
}
int main() {
char str[] = "Hello World";
toUpperCase(str);
printf("Uppercase: %s\n", str);
toLowerCase(str);
printf("Lowercase: %s\n", str);
return 0;
}
') {
str[i] = toupper(str[i]);
i++;
}
}
void toLowerCase(char str[]) {
int i = 0;
while (str[i] != '#include <stdio.h>
#include <ctype.h>
void toUpperCase(char str[]) {
int i = 0;
while (str[i] != '\0') {
str[i] = toupper(str[i]);
i++;
}
}
void toLowerCase(char str[]) {
int i = 0;
while (str[i] != '\0') {
str[i] = tolower(str[i]);
i++;
}
}
int main() {
char str[] = "Hello World";
toUpperCase(str);
printf("Uppercase: %s\n", str);
toLowerCase(str);
printf("Lowercase: %s\n", str);
return 0;
}
') {
str[i] = tolower(str[i]);
i++;
}
}
int main() {
char str[] = "Hello World";
toUpperCase(str);
printf("Uppercase: %s\n", str);
toLowerCase(str);
printf("Lowercase: %s\n", str);
return 0;
}
输出结果:
Uppercase: HELLO WORLD
Lowercase: hello world
在上面的示例中,toUpperCase()
函数将字符串中的所有字符转换为大写形式,toLowerCase()
函数将字符串中的所有字符转换为小写形式。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/836653.html