C语言实现:Given a string S, convert all lowercase letters to uppercase letters, and uppercase letters to lowercase letters in it.
时间: 2023-11-22 22:55:39 浏览: 42
可以使用C语言中的字符处理函数来实现该功能。具体实现如下:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i = 0;
printf("Enter a string: ");
fgets(str, 100, stdin); // 读取输入的字符串
while (str[i]) {
if (islower(str[i])) {
str[i] = toupper(str[i]); // 如果是小写字母,转换成大写字母
} else if (isupper(str[i])) {
str[i] = tolower(str[i]); // 如果是大写字母,转换成小写字母
}
i++;
}
printf("Converted string: %s", str);
return 0;
}
```
在该程序中,我们首先使用 `fgets()` 函数读取用户输入的字符串,并将其存储在 `str` 数组中。然后,我们遍历字符串中的每个字符,如果是小写字母,就使用 `toupper()` 函数将其转换为大写字母;如果是大写字母,就使用 `tolower()` 函数将其转换为小写字母。最后,我们输出转换后的字符串。
相关问题
Given a string S, convert all lowercase letters to uppercase letters, and uppercase letters to lowercase letters in it.
这道题可以用 Python 来实现,具体代码如下:
```python
def swap_case(s):
return s.swapcase()
string = "Hello World"
print(swap_case(string)) # 输出 hELLO wORLD
```
其中,`swapcase()` 方法可以实现大小写字母的互换。在这个例子中,我们定义了一个名为 `swap_case()` 的函数,它接受一个字符串作为参数,并返回大小写字母互换后的字符串。最后,我们定义了一个字符串 `string`,并在控制台打印出调用 `swap_case()` 函数后得到的结果。
用grep Print all lines containing an uppercase letter, followed by four lowercase letters, a comma, a , and one uppercase letter.
Sure, you can use the following command to grep for the specified pattern:
grep -E '[A-Z][a-z]{4},[a-z],[A-Z]' <filename>
This will print all lines in <filename> that contain an uppercase letter, followed by four lowercase letters, a comma, a lowercase letter, and finally an uppercase letter.
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)