编写函数char* invert_char(char s[]),输入一串字符(包括字母和其它符号),将字母中的大写转化为小写,将小写转化为大写,最后返回转换后的字符串。
时间: 2023-05-20 09:05:25 浏览: 84
以下是invert_char函数的完整代码:
```c
#include <stdio.h>
#include <string.h>
char* invert_char(char s[]) {
int len = strlen(s);
for (int i = 0; i < len; i++) {
if (s[i] >= 'a' && s[i] <= 'z') {
s[i] = s[i] - 'a' + 'A';
} else if (s[i] >= 'A' && s[i] <= 'Z') {
s[i] = s[i] - 'A' + 'a';
}
}
return s;
}
int main() {
char s[] = "Hello, World!";
printf("%s\n", invert_char(s));
return 0;
}
```
这个函数会将输入字符串中的大写字母转换为小写字母,小写字母转换为大写字母,并返回转换后的字符串。
相关问题
编写函数char*invert_char(char s[]),输入一串字符(包括字母和其他符号),将字母中的大写转化为小写,小写转化为大写,最后返回转换后的字符串
下面是invert_char函数的完整代码实现:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char* invert_char(char s[]) {
int len = strlen(s);
for (int i = 0; i < len; i++) {
if (isalpha(s[i])) {
if (islower(s[i])) {
s[i] = toupper(s[i]);
} else if (isupper(s[i])) {
s[i] = tolower(s[i]);
}
}
}
return s;
}
int main() {
char s[] = "heLLo, wORLd!";
printf("%s\n", invert_char(s));
return 0;
}
```
这个函数可以接受一个以null结尾的C字符串作为输入,然后对其中的每个字母进行大小写转换,并返回转换后的字符串。
对于输入字符串中的每个字符,我们首先使用isalpha函数来判断该字符是否为字母。如果是字母,我们再使用islower和isupper函数来判断该字符是否为小写字母或大写字母,然后使用tolower和toupper函数进行转换。最后将转换后的字符串返回即可。
对于输入字符串"heLLo, wORLd!",函数的输出结果为"HEllO, WorlD!"。
(1)编写程序,对输入的一行小写字母进行加密处理。加密方法为:每一字母用其 ASCII 码加1的字母替换,字母 z 用 a 替换。要求替换过程用函数过程实现,函数的头部为 void encrypt ( char * s )。 (2)编写程序将整型数组 a 的元素按相反顺序存放。要求按相反顺序存放用函数过程实现,函数的头部为 void invert ( int * x , int n )。
(1) 代码如下:
```c
#include <stdio.h>
#include <string.h>
void encrypt(char *s);
int main() {
char s[100];
printf("请输入一行小写字母:");
fgets(s, 100, stdin);
encrypt(s);
printf("加密后的结果为:%s", s);
return 0;
}
void encrypt(char *s) {
int len = strlen(s);
for (int i = 0; i < len; i++) {
if (s[i] == 'z') {
s[i] = 'a';
} else if (s[i] >= 'a' && s[i] < 'z') {
s[i]++;
}
}
}
```
这个程序中,我们先定义了一个 `encrypt` 函数,用于加密输入的一行小写字母。函数中用一个循环遍历整个字符串,对于每个字母,如果不是字母 z,就将其 ASCII 码加 1,然后替换原来的字母。如果是字母 z,则将其替换为字母 a。这个函数的参数是一个字符指针,指向要加密的字符串。
程序的主函数中,我们先定义了一个长度为 100 的字符数组 `s`,用于存放用户输入的一行小写字母。然后调用 `fgets` 函数从标准输入中读取这一行字母,并将其存入数组 `s` 中。接着调用 `encrypt` 函数对这一行字母进行加密,并输出加密后的结果。
(2) 代码如下:
```c
#include <stdio.h>
void invert(int *x, int n);
int main() {
int a[] = {1, 2, 3, 4, 5};
int n = sizeof(a) / sizeof(a[0]);
printf("原数组:");
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
invert(a, n);
printf("翻转后的数组:");
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
void invert(int *x, int n) {
int temp;
for (int i = 0; i < n / 2; i++) {
temp = x[i];
x[i] = x[n - i - 1];
x[n - i - 1] = temp;
}
}
```
这个程序中,我们先定义了一个 `invert` 函数,用于将整型数组 `a` 的元素按相反顺序存放。函数中用一个循环遍历数组的前一半,对于每个元素,将其与对应的相反位置的元素互换。注意,我们只需要循环数组的前一半,因为每次循环都会交换数组的两个元素,这样就可以将整个数组翻转过来。
程序的主函数中,我们先定义了一个整型数组 `a`,用于存放需要翻转的元素。然后输出原数组的内容,调用 `invert` 函数对数组进行翻转,并输出翻转后的数组内容。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)