大小写字母转换字符串
时间: 2024-01-03 10:03:58 浏览: 28
以下是Python中大小写字母转换字符串的例子:
```python
s = "Hello World! 123"
result = ""
for char in s:
if char.isupper():
result += char.lower()
elif char.islower():
result += char.upper()
else:
result += char
print(result) # 输出:hELLO wORLD! 123
```
相关问题
c++大小写字母转换字符串
以下是C++中大小写字母转换字符串的例子:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "Hello World!";
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = toupper(str[i]); // 将小写字母转换为大写字母
} else if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = tolower(str[i]); // 将大写字母转换为小写字母
}
}
cout << str << endl; // 输出:hELLO wORLD!
return 0;
}
```
c语言将字符串中的大小写字母转换
C语言提供了两个函数可以将字符串中的大小写字母进行转换,分别是toupper()和tolower()函数。其中,toupper()函数将小写字母转换成大写字母,而tolower()函数则将大写字母转换成小写字母。下面是一个将字符串中的小写字母转换成大写字母的例子:
#include <stdio.h>
#include <ctype.h>
int main() {
char s[] = "aBcDeFgH12345;!#$";
int i;
printf("before toupper() : %s\n", s);
for(i = 0; i < sizeof(s); i++) {
s[i] = toupper(s[i]);
}
printf("after toupper() : %s\n", s);
return 0;
}
在上面的例子中,我们首先定义了一个字符串s,然后使用for循环遍历字符串中的每一个字符,将小写字母转换成大写字母。最后输出转换后的字符串。