5. 在一个字符数组中存放“AbcDEfg”字符串,编写程序,把该字符串中的小写字母变为大写字母,大写字母变为小写字母。
程序代码:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void invertCase(char s[]) {
int i = 0;
while(s[i] != '\0') {
if (isupper(s[i])) {
s[i] = tolower(s[i]);
} else if (islower(s[i])) {
s[i] = toupper(s[i]);
}
i++;
}
}
int main() {
char s[] = "AbcDEfg";
printf("原始字符串为:%s\n", s);
invertCase(s);
printf("转换后字符串为:%s\n", s);
return 0;
}